簡體   English   中英

如何使用Vb.Net讀取XML文件的子級別?

[英]How to read sub-levels of XML file with Vb.Net?

大家好,我有這種情況,下面有下一個XML文件,它有2個子級別,我找不到讀取它的方法。 這是結構

<S:Envelope>
   <S:Body>
      <consultEdocument>
         <response>
            <result>
               <cove>
                  <transmitter>
                     <idetype>0</idetype>
                     <identification>58-249660700</identification>
                     <name>PRECISION INTERCONNECT INC.</name>
                     <address>
                        <street>SW FREEMAN COURT</street>
                        <ext_no>10025</ext_no>
                        <city>WILSONVILLE</city>
                        <country>USA</country>
                        <zipcode>97070-9289</zipcode>
                     </address>
                  </transmitter>
                  <addressee>
                     <idetype>1</idetype>
                     <identification>MTK861014317</identification>
                     <name>MAQUILAS TETA KAWI SA DE CV</name>
                     <address>
                        <street>GUADALAJARA-NOGALES</street>
                        <ext_no>KM 1969</ext_no>
                        <city>EMPALME</city>
                        <country>MEX</country>
                        <zipcode>85340</zipcode>
                     </address>
                  </addressee>
               </cove>
            </result>
         </response>
      </consultEdocument>
   </S:Body>
</S:Envelope>

我首先需要獲取發送者的街道以將其保存在變量中,然后需要獲取收件人的街道,但請注意,在兩種情況下,其標簽名稱均為“ street”,因此當我嘗試捕獲該值時,它將顯示NullReferenceException。 所以我不知道這是否是因為標簽名稱,這是我的代碼。

Public Sub process_xml(ByVal xmlfile As String)

        Dim xml_doc As XmlDocument
        xml_doc = New XmlDocument
        xml_doc.Load("C:\" + xmlfile)


        Dim name_space_mgr = New XmlNamespaceManager(xml_doc.NameTable)
        name_space_mgr.AddNamespace("S", "http://schemas.xmlsoap.org/soap/envelope/")
        name_space_mgr.AddNamespace("wsse", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd")
        name_space_mgr.AddNamespace("wsu", "http://schemas.xmlsoap.org/ws/2002/07/utility")


        Dim identification_type_transmitter = ""
        Dim tax_id_transmitter = ""
        Dim name_transmitter = ""
        Dim paternal_surname_transmitter = ""
        Dim maternal_surname_transmitter = ""
        Dim street_transmitter = ""
        Dim ext_num_transmitter = ""
        Dim int_num_transmitter = ""
        Dim zipcode_transmitter = ""
        Dim neighborhood_transmitter = ""
        Dim city_transmitter = ""
        Dim federal_entity_transmitter = ""
        Dim county_transmitter = ""
        Dim country_transmitter = ""

        Dim nodelist = xml_doc.GetElementsByTagName("transmitter")

        For Each node As XmlElement In nodelist
            identification_type_transmitter = node("idetype").InnerText.ToString()
            tax_id_transmitter = node("identification").InnerText.ToString()
            name_transmitter = node("name").InnerText.ToString()
        Next

        'NOTE THAT I TRIED WITH DIFFERENT WAYS AND THEY DIDN T WORK 
        Dim nodelist_transmitter = xml_doc.SelectNodes("//S:Envelope/S:Body/consultEdocument/response/result/cove/transmitter/address/street/").ItemOf(0)
        Dim nodelist_transmitter = xml_doc.SelectNodes("address").ItemOf(0)'

        Dim nodelist_transmitter = xml_doc.SelectNodes("//*[local-name()='address']").ItemOf(0)
        For Each node As XmlElement In nodelist_transmitter

            street_transmitter = node("street").InnerText.ToString() 'Here is the NullReferenceException'
            ext_num_transmitter = node("ext_no").InnerText.ToString()

            '.... another tags'
        Next

        'Make some processes with data....'
    End Sub

因此,我嘗試了一些方法,但沒有成功。.我該怎么做? 謝謝!

以下代碼用於選擇兩個元素:

Dim xml_doc As New XmlDocument()
xml_doc.Load("C:\" + xmlfile)
Dim name_space_mgr As New XmlNamespaceManager(xml_doc.NameTable)
name_space_mgr.AddNamespace("S", "http://schemas.xmlsoap.org/soap/envelope/")
Dim transmitterAddressNode As XmlNode = xml_doc.SelectSingleNode("//S:Envelope/S:Body/consultEdocument/response/result/cove/transmitter/address/street", name_space_mgr)
Dim addresseeAddressNode As XmlNode = xml_doc.SelectSingleNode("//S:Envelope/S:Body/consultEdocument/response/result/cove/addressee/address/street", name_space_mgr)

關鍵區別在於,在我的示例中,我將名稱空間管理器作為參數傳遞給select方法。

(此外,為了使其正常工作,我必須將名稱空間聲明插入示例XML文檔中: <S:Envelope xmlns:S='http://schemas.xmlsoap.org/soap/envelope/'> 。否則,XML文檔的格式將不正確。)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM