2015-06-24 145 views
1

我目前正试图用VB.net解析数据来填充一些由子名“eResponse.01”,02,03等选择的文本框,但是主标签中的名称空间/模式位置似乎跳过代码。使用以下用vb.net解析xml数据

<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> 

时,但它不填充任何东西,如果我包括命名空间/模式

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 

工作正常

<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> 

什么,我需要做的就是我的代码忽略开始标记中的额外数据 - 删除该信息不是一个选项。

+0

可能重复(http://stackoverflow.com/questions/6275837/add-namespace-using-xmlnamespacemanager-in-c-sharp) – Tim

+1

您需要使用'XmlNamespaceManager' - 在我的重复评论中链接了一个示例。 – Tim

+0

我appologize我复制旧代码 - 我已经尝试namespacemanager并且要么失败的语法,要么不明白它 - 编辑,以显示什么使用 –

回答

1

你的XML有前缀的命名空间 - 也被称为默认命名空间 - 在这里:

xmlns="http://www.nemsis.org" 

不像前缀名字空间,后代元素继承祖先的默认命名空间隐含

要访问命名空间中的元素,你需要在你的XPath正确使用注册的前缀,并通过命名空间管理为SelectNodes()第二个参数和SelectSingleNode():的[在C#中添加使用的XmlNamespaceManager命名空间]

...... 
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 
+0

我曾尝试此更早但收到一个错误,指出System.ArgumentException:前缀“xmlns”是保留供XML使用。 at EPC.Form1.Form1_Load(Object sender,EventArgs e)in System.Xml.XmlNamespaceManager.AddNamespace(String prefix,String uri) F:\ FTDEPCR \ EPCR \ EPCR \ Form1.vb:第47行 –

+1

现在编辑完美的作品。 –

+0

我不知道'xmlns'是保留的,认为任何前缀都可以,只要它指向正确的uri – har07