2

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
1
  • you need to filter by section where section attribute is = to Building Commented Jul 9, 2014 at 20:43

3 Answers 3

2

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
Sign up to request clarification or add additional context in comments.

1 Comment

Wow. That is so easy. I thought there had to be a way to specify inline, but I didn't know how.
0

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

Comments

0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.