Kevin_
Kevin_

Reputation: 3096

Parse XML using XMLDocument

I am trying to parse an XML file that is setup like so:

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

I have been following this article http://www.codeproject.com/Articles/4826/XML-File-Parsing-in-VB-NET

I can read the file, and I figured out how to get all of the item nodes, however I can't figure out how to get the items that are in a single section.

For example, I am trying to get all items that are in the section where the category is Building.

This is what I have so far...

    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

Upvotes: 2

Views: 1659

Answers (3)

har07
har07

Reputation: 89285

Yes, there is a cleaner way using XPath only to filter node by attribute :

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

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

Upvotes: 2

keenthinker
keenthinker

Reputation: 7830

There are a lot of options. One possibility is to use LINQ2XML, which allows powerful constructs in 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

The output is:

1PE
SL1
LR1
LL8

If you want to load the XML structure from a file, use 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

Upvotes: 0

Kevin_
Kevin_

Reputation: 3096

Based on the comment from makemone2010, I did find a working solution.

I'm not sure if there is a better way but it works.

    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

Upvotes: 0

Related Questions