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:
When I run the code it returns the following: "Node Name: 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"
Below is the code to write the XML in case maybe somethings wrong there:
Any help would be greatly appreciated Tom
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
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
Hirendra SisodiyaPosted Feb 8, 2011, 7:17 AM
you need to value of name ei. "\\.\PHYSIVALDRIVE0", "\\.\PHYSIVALDRIVE1" or \\.\PHYSIVALDRIVE2
these are values of attribute 'name' of elememt 'Instance' . so you need to apply this 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 instance As XmlNode In node.ChildNodes
For i As Integer = 0 To instance.Attributes.Count - 1
Dim A As XmlAttribute = instance.Attributes.Item(i)
Console.WriteLine(A.Value)
Next
thanks
Plese mark as answer if it helps
Next
End If
Thomas TraylorPosted Feb 8, 2011, 11:29 AM