使用 vb.net 解析 xml 数据 [英] parsing xml data with vb.net
问题描述
我目前正在尝试使用 VB.net 解析数据,以填充一些通过子名称eResponse.01"、02、03 等选择的文本框,但是主标记中的命名空间/架构位置似乎会绊倒代码.
I am currently attempting to parse data with VB.net to populate some textboxes selecting by childname "eResponse.01", 02, 03 etc however a namespace / schema location in the main tag seems to be tripping up the code.
Dim xmlDoc As New XmlDocument()
xmlDoc.Load("C:\Users\james\Desktop\NEMSIS\EMS\xml\Test.xml")
Dim xmlns As New XmlNamespaceManager(xmlDoc.NameTable)
xmlns.AddNamespace("xsi", "http://www1w3.org/2001/XMLSchema-instance")
xmlns.AddNamespace("schemaLocation", "http://www.nemsis.org http://nemsis.org/media/nemsis_v3/release-3.4.0/XSDs/NEMSIS_XSDs/EMSDataSet_v3.xsd")
xmlns.AddNamespace("xmlns", "http://www.nemsis.org")
Dim nodes As XmlNodeList = xmlDoc.DocumentElement.SelectNodes("/EMSDataSet/Header/PatientCareReport/eResponse")
For Each node As XmlNode In nodes
TextEdit1.Text = node.SelectSingleNode("eResponse.03").InnerText
Next
使用以下内容时工作正常
works fine when using the following
<EMSDataSet>
<Header>
<DemographicGroup>
<dAgency.01>0</dAgency.01>
<dAgency.02>00</dAgency.02>
<dAgency.04>49</dAgency.04></DemographicGroup>
<PatientCareReport>
<eRecord>
<eRecord.01>OpP</eRecord.01>
<eRecord.SoftwareApplicationGroup>
<eRecord.02>G</eRecord.02>
<eRecord.03>Q</eRecord.03>
<eRecord.04>P</eRecord.04></eRecord.SoftwareApplicationGroup></eRecord>
<eResponse>
<eResponse.AgencyGroup>
<eResponse.01>a</eResponse.01>
<eResponse.02>BL</eResponse.02></eResponse.AgencyGroup>
<eResponse.03>u33</eResponse.03>
但是,如果我包含命名空间/模式,它不会填充任何内容
however it does not populate anything if I include the namespace/schema
<EMSDataSet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.nemsis.org http://nemsis.org/media/nemsis_v3/release-3.4.0/XSDs/NEMSIS_XSDs/EMSDataSet_v3.xsd" xmlns="http://www.nemsis.org">
<Header>
<DemographicGroup>
<dAgency.01>0</dAgency.01>
<dAgency.02>00</dAgency.02>
<dAgency.04>49</dAgency.04></DemographicGroup>
<PatientCareReport>
<eRecord>
<eRecord.01>OpP</eRecord.01>
<eRecord.SoftwareApplicationGroup>
<eRecord.02>G</eRecord.02>
<eRecord.03>Q</eRecord.03>
<eRecord.04>P</eRecord.04></eRecord.SoftwareApplicationGroup></eRecord>
<eResponse>
<eResponse.AgencyGroup>
<eResponse.01>a</eResponse.01>
<eResponse.02>BL</eResponse.02></eResponse.AgencyGroup>
<eResponse.03>u33</eResponse.03>
我需要做什么才能让我的代码忽略开始标记中的额外数据 - 删除该信息不是一种选择.
what do I need to do to get my code to ignore extra data in the opening tag - removing that information is not an option.
推荐答案
您的 XML 具有不带前缀的命名空间 - 也称为 默认命名空间- 这里:
Your XML has unprefixed namespace -also known as default namespace- here :
xmlns="http://www.nemsis.org"
与前缀命名空间不同,后代元素隐式继承祖先默认命名空间.
unlike prefixed namespace, descendant elements inherit ancestor default namespace implicitly.
要访问命名空间中的元素,您需要在 XPath 中正确使用注册前缀,并将命名空间管理器作为 SelectNodes()
和 SelectSingleNode()
的第二个参数传递:
To access elements in namespace, you need to use registered prefix properly in your XPath and pass the namespace manager as 2nd argument of SelectNodes()
and SelectSingleNode()
:
......
xmlns.AddNamespace("d", "http://www.nemsis.org")
Dim xpath As String = "/d:EMSDataSet/d:Header/d:PatientCareReport/d:eResponse"
Dim nodes As XmlNodeList = xmlDoc.DocumentElement.SelectNodes(xpath, xmlns)
For Each node As XmlNode In nodes
TextEdit1.Text = node.SelectSingleNode("d:eResponse.03", xmlns).InnerText
Next
这篇关于使用 vb.net 解析 xml 数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!