INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

XML nodes and attributes

XML nodes and attributes

(OP)
Hi, I have the following code that reads an XML file and pulls out attributes for a specific node to a listview control, which works as expected. I am now trying to also pull out specific node data <Words> and <Segments> from under the <Total> element and add to the listview. I was wondering, do I need to run separate loops on the XML, one to get the attributes and one to get the <Words> and <Segments> data.

Here is the code

CODE -->

Dim loadDoc As New System.Xml.XmlDocument()
        loadDoc.Load("C:\test\analysis.xml")
        For Each ipNode As System.Xml.XmlNode In loadDoc.SelectNodes("/Report/Analysis/Project/Totals/TMs/TM")
            Dim item As ListViewItem = ListView1.Items.Add(ipNode.Attributes("Type").InnerText)
            item.SubItems.Add(ipNode.Attributes("Name").InnerText)
            item.SubItems.Add(ipNode.Attributes("MemoryID").InnerText)
        Next 

Here is a sample of the XML

<?xml version="1.0" encoding="utf-8"?>
<Report xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="AnalysisReport.xsd">
<Analysis Date="20150219T184601Z">
<Project SourceLang="en-us">

<Totals>

<TMs>
<TM Type="Fg" Name="Main-Power" MemoryID="Z1">
<CharsPerWord>5.75</CharsPerWord>

<Total>
<Characters>92</Characters>
<Words>16</Words>
<Segments>6</Segments>
<MeteredWords>16</MeteredWords>

</Total>
</TM>
<TM Type="Bg" Name="Minor-Power" MemoryID="9815562">
<CharsPerWord>4.63</CharsPerWord>

<Total>
<Characters>176</Characters>
<Words>38</Words>
<Segments>19</Segments>
<MeteredWords>38</MeteredWords>

</Total>
</TM>

</TMs>
</Totals>
</Project>
</Analysis>
</Report>

From what I have seen on the net, there are many different ways to read XML files and pull out specific data and to be honest, it seems a bit daunting on how to pull out the data I need.

I would appreciate any help with this.

Thanks

RE: XML nodes and attributes


This will do it:

CODE

Dim loadDoc As New System.Xml.XmlDocument()
        Dim item As ListViewItem

        Dim TotalNode As System.Xml.XmlNode
        Dim TotalSubNode As System.Xml.XmlNodeList

        loadDoc.Load("C:\temp\test.xml")

        For Each ipNode As System.Xml.XmlNode In loadDoc.SelectNodes("/Report/Analysis/Project/Totals/TMs/TM")
            item = New ListViewItem(ipNode.Attributes("Type").InnerText)
            item.SubItems.Add(ipNode.Attributes("Name").InnerText)
            item.SubItems.Add(ipNode.Attributes("MemoryID").InnerText)

            For Each TotalNode In ipNode

                If TotalNode.Name = "Total" Then

                    TotalSubNode = TotalNode.SelectNodes("Words")

                    For Each n As System.Xml.XmlNode In TotalSubNode
                        item.SubItems.Add(n.InnerText)
                    Next

                    TotalSubNode = TotalNode.SelectNodes("Segments")

                    For Each n As System.Xml.XmlNode In TotalSubNode
                        item.SubItems.Add(n.InnerText)
                    Next

                End If

            Next

            ListView1.Items.Add(item)
        Next 

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!

RE: XML nodes and attributes

(OP)
Thank You for the code, it works great.

Thanks

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close