如何使用Vb.Net读取XML文件的子级别?

时间:2014-01-27 17:52:36

标签: xml vb.net xmldocument

每个人的好日子,我有这种情况,我有下面的下一个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

所以我尝试了一些方法,但没有成功..我怎么能这样做? 谢谢你!

1 个答案:

答案 0 :(得分:0)

以下代码用于选择两个元素:

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文档格式不正确。)