[英]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.