Thomas Traylor

Thomas Traylor

  • NA
  • 3
  • 9.5k

Reading XML childnodes in VB 2010

Feb 7 2011 12:46 PM

Hello there,

 

I am new to XML. Trying to read the childnode "instance". This has been kicking my butt and I can't seem to figure it out. Here is a snippet of the XML file:

 

 

  <?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="512" FirmwareRevision="3.AHG" InterfaceType="IDE" Model="ST3320820AS ATA Device" Partitions="1" SCSIPort="1" SectorsPerTrack="63" />
    </instance>
    - <instance Name="\\.\PHYSICALDRIVE1">
        <properties BytesPerSector="512" FirmwareRevision="8.01" InterfaceType="IDE" Model="ST380013A ATA Device" Partitions="2" SCSIPort="0" SectorsPerTrack="63" />
    </instance>
    - <instance Name="\\.\PHYSICALDRIVE2">
        <properties BytesPerSector="512" FirmwareRevision="8.01" InterfaceType="IDE" Model="ST380013A ATA Device" Partitions="2" SCSIPort="0" SectorsPerTrack="63" />
  </instance>
 </class>
</root>

 

 

When I run the code it returns the following:

 

"Node Name: <properties BytesPerSector="512" FirmwareRevision="3.AHG" InterfaceType="IDE" Model="ST3320820AS ATA Device" Partitions="1" SCSIPort="1" SectorsPerTrack="63" />"

 

I am trying to get it to return the values of the "instance" node (ei. "\\.\PHYSIVALDRIVE0", "\\.\PHYSIVALDRIVE1" or "\\.\PHYSIVALDRIVE2"

Here is the code. I know the For..Next loop is correct because it does loop 3 times and that's how many "instance" nodes exist, but I can't get it to return the values of "instance"

 

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 instance As XmlNode In node.ChildNodes
Console.WriteLine("Node Name: " & instance.InnerXml)
Next
End If
 

 

Below is the code to write the XML in case maybe somethings wrong there:

 

Dim XMLFile As String = "C:\Temp\XML\TestData.xml"
Dim writer As New XmlTextWriter(XMLFile, System.Text.Encoding.UTF8)
Try
writer.WriteStartDocument()
writer.Formatting =
Formatting.Indented
writer.Indentation = 5
writer.WriteComment(
"TestData.xml stores hardware and software configuration information from the Win32 Classes of the WMI root\CIMV2 namespace")
writer.WriteStartElement(
"root") 'create root node
writer.WriteStartElement(
"class") 'create class node
writer.WriteAttributeString(
"Name", "Win32_DiskDrive")
writer.WriteStartElement(
"instance") 'create instance node
writer.WriteAttributeString(
"Name", "\\.\PHYSICALDRIVE0")
writer.WriteStartElement(
"properties") 'create properties node
writer.WriteAttributeString(
"BytesPerSector", "512")
writer.WriteAttributeString(
"FirmwareRevision", "3.AHG")
writer.WriteAttributeString(
"InterfaceType", "IDE")
writer.WriteAttributeString(
"Model", "ST3320820AS ATA Device")
writer.WriteAttributeString(
"Partitions", "1")
writer.WriteAttributeString(
"SCSIPort", "1")
writer.WriteAttributeString(
"SectorsPerTrack", "63")
writer.WriteEndElement()
'close the instance node
writer.WriteEndElement()
'close the class node
writer.WriteStartElement(
"instance") 'create instance node
writer.WriteAttributeString(
"Name", "\\.\PHYSICALDRIVE1")
writer.WriteStartElement(
"properties") 'create properties node
writer.WriteAttributeString(
"BytesPerSector", "512")
writer.WriteAttributeString(
"FirmwareRevision", "8.01")
writer.WriteAttributeString(
"InterfaceType", "IDE")
writer.WriteAttributeString(
"Model", "ST380013A ATA Device")
writer.WriteAttributeString(
"Partitions", "2")
writer.WriteAttributeString(
"SCSIPort", "0")
writer.WriteAttributeString(
"SectorsPerTrack", "63")
writer.WriteEndElement()
'close the instance node
writer.WriteEndElement()
'close the class node
writer.WriteStartElement(
"instance") 'create instance node
writer.WriteAttributeString(
"Name", "\\.\PHYSICALDRIVE2")
writer.WriteStartElement(
"properties") 'create properties node
writer.WriteAttributeString(
"BytesPerSector", "512")
writer.WriteAttributeString(
"FirmwareRevision", "8.01")
writer.WriteAttributeString(
"InterfaceType", "IDE")
writer.WriteAttributeString(
"Model", "ST380013A ATA Device")
writer.WriteAttributeString(
"Partitions", "2")
writer.WriteAttributeString(
"SCSIPort", "0")
writer.WriteAttributeString(
"SectorsPerTrack", "63")
writer.WriteEndElement()
'close the instance node
writer.WriteEndElement()
'close the class node
writer.WriteEndElement()
'close the root node
writer.Flush()
writer.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try

 

Any help would be greatly appreciated

 

TIA,

 

Tom

 


Answers (2)