使用XMLDocument解析XML

时间:2014-07-09 20:29:47

标签: xml vb.net

我正在尝试解析一个设置如下的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

我可以阅读该文件,并且我想出了如何获取所有项目节点,但我无法弄清楚如何获取单个部分中的项目

例如,我正在尝试获取类别所在的部分中的所有项目。

这就是我到目前为止......

    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

3 个答案:

答案 0 :(得分:2)

是的,有一种更简洁的方法,只使用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

答案 1 :(得分:0)

根据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

答案 2 :(得分:0)

有很多选择。一种可能性是使用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