I am trying to use the following code
Imports System.Xml
Public Class Form1
Private Sub Convert_Button_Click(sender As Object, e As EventArgs) Handles Convert_Button.Click
Dim doc As New XmlDocument()
doc.Load("C:\Test\Inventory.xml")
Dim nodes As XmlNodeList = doc.DocumentElement.SelectNodes("/IXFleet/SyncData/Transaction")
Dim product_id As String = "", product_name As String = "", product_price As String = ""
For Each node As XmlNode In nodes
product_id = node.SelectSingleNode("SiteID").InnerText
product_name = node.SelectSingleNode("TankID").InnerText
product_price = node.SelectSingleNode("TankNumber").InnerText
MessageBox.Show(product_id & " " & product_name & " " & product_price)
Next
End Sub
End Class
And I am trying to read the following XML data
<IXFleet>
<SyncConfig xmlns="http://tempuri.org/SyncConfig.xsd"/>
<SyncData xmlns="http://tempuri.org/SyncData.xsd">
<Transaction>
<SiteID>1</SiteID>
<TankID>1</TankID>
<TankNumber>1</TankNumber>
</Transaction>
</SyncData>
</IXFleet>
My problem is I dont know how to format this section of code properly (specifically the SyncData part of the tag path) to actually get to the Transaction node, to read SiteID, TankID, and TankNumber
Dim nodes As XmlNodeList = doc.DocumentElement.SelectNodes("/IXFleet/SyncData/Transaction")
If I manually remove the extra data (xmlns="http://tempuri.org/SyncData.xsd") in the XML file, SyncData node, and use the path as is in the VB code, it works, put the extra data back into the XML and it fails and cant find any data at all.
Any help would sure be appreciated.
You need a NamespaceManager
. Then execute all queries with namespace prefixes and pass in the manager.
Dim doc As New XmlDocument()
doc.Load("C:\Test\Inventory.xml")
Dim nsmgr = New XmlNamespaceManager(doc.NameTable)
nsmgr.AddNamespace("sd", "http://tempuri.org/SyncData.xsd")
Dim nodes As XmlNodeList = doc.DocumentElement.SelectNodes("/IXFleet/sd:SyncData/sd:Transaction", nsmgr)
Dim product_id As String = "", product_name As String = "", product_price As String = ""
For Each node As XmlNode In nodes
product_id = node.SelectSingleNode("sd:SiteID", nsmgr).InnerText
product_name = node.SelectSingleNode("sd:TankID", nsmgr).InnerText
product_price = node.SelectSingleNode("sd:TankNumber", nsmgr).InnerText
MessageBox.Show(product_id & " " & product_name & " " & product_price)
Next
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.