【问题标题】:Parse XML using XMLDocument使用 XMLDocument 解析 XML
【发布时间】:2014-07-09 20:29:47
【问题描述】:

我正在尝试解析这样设置的 XML 文件:

<root>
<Section category="Device_Type" CodeLength="1">
    <item code="C">Cart</item>
    <item code="D">Desktop</item>
    <item code="L">Laptop</item>
    <item code="T">Tablets</item>
    <item code="V">Virtual</item>
    <item code="R">Robobox</item>
</Section>
<Section category="Building" CodeLength="3">
    <item code="1PE">Address</item>
    <item code="SL1">Address</item>
    <item code="LR1">Address</item>
    <item code="LL8">Address</item>
    ...
</Section>

我一直在关注这篇文章http://www.codeproject.com/Articles/4826/XML-File-Parsing-in-VB-NET

我可以读取文件,并且知道如何获取所有项目节点,但是 我不知道如何获取单个部分中的项目。 p>

例如,我试图获取类别为“建筑”的部分中的所有项目。

这就是我目前所拥有的......

    Private Sub TabItem_Loaded(sender As Object, e As RoutedEventArgs)
    Dim XMLDoc As New Xml.XmlDocument
    Dim Nodelist As Xml.XmlNodeList
    Dim Node As Xml.XmlNode

    XMLDoc.Load("\\ukhcdata\share\ITS Shared Files\Rename Computer XML\NamingStandardsCode.xml")
    Nodelist = XMLDoc.SelectNodes("/root/Section/item")

    For Each Node In Nodelist
        Dim itemCode = Node.Attributes.GetNamedItem("code").Value
        MsgBox(itemCode.ToString)
    Next
End Sub

【问题讨论】:

  • 您需要按section属性为= to Building的section进行过滤

标签: xml vb.net


【解决方案1】:

是的,有一种更简洁的方法,只使用 XPath 按属性过滤节点:

Nodelist = XMLDoc.SelectNodes("/root/Section[@category='Building']/item")

For Each Node In Nodelist
    Dim itemCode = Node.Attributes.GetNamedItem("code").Value
    MsgBox(itemCode.ToString)
Next

【讨论】:

  • 哇。那太容易了。我认为必须有一种方法来指定内联,但我不知道如何。
【解决方案2】:

根据 makemone2010 的评论,我确实找到了可行的解决方案。

我不确定是否有更好的方法,但它确实有效。

    Private Sub TabItem_Loaded(sender As Object, e As RoutedEventArgs)
    Dim XMLDoc As New Xml.XmlDocument
    Dim Nodelist As Xml.XmlNodeList
    Dim Node As Xml.XmlNode

    XMLDoc.Load("\\ukhcdata\share\ITS Shared Files\Rename Computer XML\NamingStandardsCode.xml")
    Nodelist = XMLDoc.SelectNodes("/root/Section/item")

    For Each Node In Nodelist
        If Node.ParentNode.Attributes.GetNamedItem("category").Value = "Building" Then
            Dim itemCode = Node.Attributes.GetNamedItem("code").Value
            MsgBox(itemCode.ToString)
        End If
    Next
End Sub

【讨论】:

    【解决方案3】:

    有很多选择。一种可能性是使用LINQ2XML,它允许在 VB.NET 中进行强大的构造:

        Dim xml = <root>
        <Section category="Device_Type" CodeLength="1">
            <item code="C">Cart</item>
            <item code="D">Desktop</item>
            <item code="L">Laptop</item>
            <item code="T">Tablets</item>
            <item code="V">Virtual</item>
            <item code="R">Robobox</item>
        </Section>
        <Section category="Building" CodeLength="3">
            <item code="1PE">Address</item>
            <item code="SL1">Address</item>
            <item code="LR1">Address</item>
            <item code="LL8">Address</item>
        </Section>
    </root>
        ' take correct section
        dim section = (from e in xml.Elements("Section") where e.Attribute("category").Value = "Building").FirstOrDefault()
        for each node in section.Elements("item")
            Console.WriteLine(node.Attribute("code").Value)
        next node
    

    输出是:

    1PE
    SL1
    LR1
    LL8
    

    如果要从文件中加载 XML 结构,请使用XDocument.Load

        dim xml = XDocument.Load("c:\path\file.xml")
        ' take correct section
        dim section = (from e in xml.Elements("Section") where e.Attribute("category").Value = "Building").FirstOrDefault()
        for each node in section.Elements("item")
            Console.WriteLine(node.Attribute("code").Value)
        next node
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-03
      • 1970-01-01
      • 2019-03-17
      相关资源
      最近更新 更多