繁体   English   中英

用vb.net解析xml数据

[英]parsing xml data with vb.net

我目前正在尝试使用VB.net解析数据,以填充一些由子名称“ eResponse.01”,02、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>
<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>

但是,如果我包含名称空间/模式,它不会填充任何内容

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

我该怎么做才能使我的代码忽略开头标签中的多余数据-删除该信息不是一种选择。

您的XML具有无前缀的名称空间-也称为默认名称空间 -此处:

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

与前缀名称空间不同,后代元素隐式继承祖先默认名称空间。

要访问命名空间中的元素,您需要在XPath中正确使用注册的前缀,并将命名空间管理器作为SelectNodes()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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM