简体   繁体   English

使用VB.net格式化正确的路径以读取xml标记

[英]Formatting the correct path to read an xml tag using VB.net

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 我正在尝试阅读以下XML数据

<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 我的问题是我不知道如何正确格式化这部分代码(特别是标记路径的SyncData部分)实际到达Transaction节点,读取SiteID,TankID和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. 如果我手动删除XML文件,SyncData节点中的额外数据(xmlns =“http://tempuri.org/SyncData.xsd”),并使用VB代码中的路径,它可以工作,放入额外的数据回到XML,它失败了,根本找不到任何数据。

Any help would sure be appreciated. 任何帮助肯定会受到赞赏。

You need a NamespaceManager . 你需要一个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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM