borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2011 Andrea Tincaniborder

AndreaVB | Forum | News | Downloads | Register | Help | Member List | Statistics | Search | PM | Profile

Print This Topic
Next Topic (HTML to PDF Howto?) New Topic New Poll Post Reply
AndreaVB Forum : VB.Net : Visual Basic 2010 - XML - Looping through childnodes Solved Topic
Poster Message
Thomas1029
Level: Trainee

Registered: 09-02-2011
Posts: 2

Ads by Lake Quincy Media
icon Visual Basic 2010 - XML - Looping through childnodes

This is an excert of the XML file I'm working with:

  <?xml version="1.0" encoding="utf-8" ?>
- <!-- TestData.xml stores hardware and software configuration information from the Win32 Classes of the WMI root\CIMV2 namespace
  -->
- <root>
- <class Name="Win32_DiskDrive">
- <instance Name="\\.\PHYSICALDRIVE0">
  <properties BytesPerSector="510" FirmwareRevision="3.AHG" InterfaceType="IDE" Model="ST3320820AS ATA Device" Partitions="1" SCSIPort="1" SectorsPerTrack="63" />
  </instance>
- <instance Name="\\.\PHYSICALDRIVE1">
  <properties BytesPerSector="511" FirmwareRevision="8.01" InterfaceType="IDE" Model="ST380013A ATA Device" Partitions="2" SCSIPort="0" SectorsPerTrack="63" />
  </instance>
- <instance Name="\\.\PHYSICALDRIVE2">
  <properties BytesPerSector="512" FirmwareRevision="8.11" InterfaceType="IDE" Model="ST380013A ATA Device" Partitions="2" SCSIPort="0" SectorsPerTrack="63" />
  </instance>
  </class>
  </root>

I'm tring to loop through each instance node ("\\.\PHYSICALDRIVE0", "\\.\PHYSICALDRIVE1" and "\\.\PHYSICALDRIVE2") and return the properties of these instances.

The .FirstChild line dose return the first property (BytesPerSector) but I can seem to read the remaining properties.

Below is a copy of my code:

        Dim sClass As String = "Win32_DiskDrive"
        Dim XMLFile As String = "C:\Temp\XML\TestData.xml"
        Dim xmlDoc As New XmlDocument

        xmlDoc.Load(XMLFile)

        Dim node As XmlNode = xmlDoc.SelectSingleNode("/root/class[@Name='" & sClass & "']")

        If node IsNot Nothing Then

            For Each sInstance As XmlNode In node.ChildNodes
                For i As Integer = 0 To sInstance.Attributes.Count - 1
                    Dim CurrentInstance As XmlAttribute = sInstance.Attributes.Item(i)
                    Console.WriteLine(CurrentInstance.Value)

                    For Each sProperty As XmlNode In node.FirstChild
                        Dim CurrentProperty As XmlAttribute = sProperty.Attributes.Item(0)
                        Console.WriteLine(CurrentProperty.Name)
                        Console.WriteLine(CurrentProperty.Value)

                        Dim nextNode As XmlNode = CurrentProperty.NextSibling
                        For p As Integer = 0 To nextNode.Attributes.Count - 1
                            Dim NextProperty As XmlAttribute = sProperty.Attributes.Item(p)
                            Console.WriteLine(NextProperty.Name)
                            Console.WriteLine(NextProperty.Value)
                        Next
                    Next
                Next
            Next
        End If

Thanks in advance for any help I can get on this issue.

Thomas Traylor

09-02-2011 at 10:04 PM
View Profile Send Email to User Show All Posts | Quote Reply
Thomas1029
Level: Trainee

Registered: 09-02-2011
Posts: 2
icon Re: Visual Basic 2010 - XML - Looping through childnodes

I got it figured out:

        Dim sClass As String = "Win32_DiskDrive"
        Dim XMLFile As String = "C:\Temp\XML\TestData.xml"
        Dim xmlDoc As New XmlDocument

        xmlDoc.Load(XMLFile) 'opens XML file
        Dim node As XmlNode = xmlDoc.SelectSingleNode("/root/class[@Name='" & sClass & "']") 'select "sClass" node

        If node IsNot Nothing Then
            For Each sInstance As XmlNode In node.ChildNodes
                For i As Integer = 0 To sInstance.Attributes.Count - 1
                    Dim CurrentInstance As XmlAttribute = sInstance.Attributes.Item(i)
                    Console.WriteLine(CurrentInstance.Value)

                    For Each sProperty As XmlNode In sInstance.ChildNodes
                        For p As Integer = 0 To sProperty.Attributes.Count - 1
                            Dim CurrentProperty As XmlAttribute = sProperty.Attributes.Item(p)
                            Console.WriteLine(CurrentProperty.Name)
                            Console.WriteLine(CurrentProperty.Value)
                        Next
                    Next
                Next
            Next
        End If
        End

10-02-2011 at 03:20 PM
View Profile Send Email to User Show All Posts | Quote Reply
AndreaVB Forum : VB.Net : Visual Basic 2010 - XML - Looping through childnodes Solved Topic
Next Topic (HTML to PDF Howto?) New Topic New Poll Post Reply
Surf To:


Not Logged In? Username: Password: Lost your password?
Partners: Download Actual Software | Free Software Download
borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2011 Andrea Tincaniborder