Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XML nodes and attributes

Status
Not open for further replies.

hugh999

MIS
Nov 29, 2001
129
0
0
IE
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=" 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
 

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!
 
Thank You for the code, it works great.

Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top