borderAndreaVB free resources for Visual Basic developersborder
borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2014 Andrea Tincaniborder

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

Print This Topic
Previous Topic (Srray Formula or any other Formula)Next Topic (Need Help) 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

icon Visual Basic 2010 - XML - Looping through childnodes

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

  
-
-
-
-
  
  

-
  
  

-
  
  

  

  


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
Previous Topic (Srray Formula or any other Formula)Next Topic (Need Help) New Topic New Poll Post Reply
Surf To:


Not Logged In? Username: Password: Lost your password?
borderAndreaVB free resources for Visual Basic developersborder
borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2014 Andrea Tincaniborder