如何从 c# 访问元素或节点到 xml 文件
本文关键字:节点 xml 文件 元素 访问 | 更新日期: 2023-09-27 18:35:14
我有这个 xml 文件:
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="CDASchemas'cda'Schemas'CCD.xsl"?>
<!-- The following sample document depicts a fictional character’s health summary. Any resemblance to a real person is coincidental. -->
<ClinicalDocument xmlns="urn:hl7-org:v3" xmlns:voc="urn:hl7-org:v3/voc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:hl7-org:v3 CDA.xsd">
<!--
********************************************************
CDA Header
********************************************************
-->
<typeId root="2.16.840.1.113883.1.3" extension="POCD_HD000040"/>
<templateId root="2.16.840.1.113883.10.20.1"/> <!-- CCD v1.0 Templates Root -->
<!--
********************************************************
CDA Body
********************************************************
-->
<component>
<structuredBody>
<!--
********************************************************
Problems section
********************************************************
-->
<component>
<section>
<templateId root='2.16.840.1.113883.10.20.1.11'/> <!-- Problem section template -->
<code code="11450-4" codeSystem="2.16.840.1.113883.6.1"/>
<title>Problems</title>
<text>
<table border="1" width="100%">
<thead>
<tr><th>Condition</th><th>Effective Dates</th><th>Condition Status</th></tr>
</thead>
<tbody>
<tr><td>Asthma</td><td>1950</td><td>Active</td></tr>
<tr><td>Pneumonia</td><td>Jan 1997</td><td>Resolved</td></tr>
<tr><td>"</td><td>Mar 1999</td><td>Resolved</td></tr>
<tr><td>Myocardial Infarction</td><td>Jan 1997</td><td>Resolved</td></tr>
</tbody>
</table>
</text>
<entry typeCode="DRIV">
<act classCode="ACT" moodCode="EVN">
<templateId root='2.16.840.1.113883.10.20.1.27'/> <!-- Problem act template -->
<id root="6a2fa88d-4174-4909-aece-db44b60a3abb"/>
<code nullFlavor="NA"/>
<entryRelationship typeCode="SUBJ">
<observation classCode="OBS" moodCode="EVN">
<templateId root='2.16.840.1.113883.10.20.1.28'/> <!-- Problem observation template -->
<id root="d11275e7-67ae-11db-bd13-0800200c9a66"/>
<code code="ASSERTION" codeSystem="2.16.840.1.113883.5.4"/>
<statusCode code="completed"/>
<effectiveTime><low value="1950"/></effectiveTime>
<value xsi:type="CD" code="195967001" codeSystem="2.16.840.1.113883.6.96" displayName="Asthma"/>
<entryRelationship typeCode="REFR">
<observation classCode="OBS" moodCode="EVN">
<templateId root='2.16.840.1.113883.10.20.1.50'/> <!-- Problem status observation template -->
<code code="33999-4" codeSystem="2.16.840.1.113883.6.1" displayName="Status"/>
<statusCode code="completed"/>
<value xsi:type="CE" code="55561003" codeSystem="2.16.840.1.113883.6.96" displayName="Active"/>
</observation>
</entryRelationship>
</observation>
</entryRelationship>
</act>
</entry>
</section>
</component>
<section>
<templateId root="2.16.840.1.113883.10.20.1.10"/> <!-- Plan of Care section template -->
<code code="18776-5" codeSystem="2.16.840.1.113883.6.1"/>
<title>Plan</title>
<text>
<table border="1" width="100%">
<thead>
<tr><th>Planned Activity</th><th>Planned Date</th></tr>
</thead>
<tbody>
<tr><td>Pulmonary function test</td><td>April 21, 2000</td></tr>
</tbody>
</table>
</text>
<entry typeCode="DRIV">
<observation classCode="OBS" moodCode="RQO">
<templateId root="2.16.840.1.113883.10.20.1.25"/> <!-- Plan of Activity activity template -->
<id root="9a6d1bac-17d3-4195-89a4-1121bc809b4a"/>
<code code="23426006" codeSystem="2.16.840.1.113883.6.96" displayName="Pulmonary function test"/>
<statusCode code="new"/>
<effectiveTime><center value="20000421"/></effectiveTime>
</observation>
</entry>
</section>
</component>
</structuredBody>
</component>
</ClinicalDocument>
我想解析这个文件,我想访问它的某些元素。所以我构建了这段代码:
class Program
{
static String _TEMPLATE_ID_PROBLEM = "2.16.840.1.113883.10.20.1.11";
static String _xmlName = "D:''Copy michele.castriotta@eresult (3).it''Projects''[2015 A] Decipher PCP''02 - phase 1''cda ccd''HL7_CCD_final''SampleCCDDocument.xml";
static void Main(string[] args)
{
var doc = XDocument.Load(_xmlName);
XElement root = doc.Root;
//DEVO CERCARE DI SELEZIONARE LA SEZIONE GIUSTA
IEnumerable<XElement>lista= root.XPathSelectElements("component/structuredBody/component/section");
foreach (XElement e in lista)
{
//to do
}
}
}
但是列表"Lista"每次都是空的。错误在哪里?
问题出在命名空间上。根下的所有元素都取自根上标记的同一命名空间。
如果您已经安装了 XPathInfo for Visual Studio,然后从 VS 打开 xml 文件,您会注意到该部分的 xpath 信息如下所示:
/*[local-name()='ClinicalDocument'][namespace-uri()='urn:hl7-org:v3']
/*[local-name()='component'][namespace-uri()='urn:hl7-org:v3']
/*[local-name()='structuredBody'][namespace-uri()='urn:hl7-org:v3']
/*[local-name()='component'][namespace-uri()='urn:hl7-org:v3']
/*[local-name()='section'][namespace-uri()='urn:hl7-org:v3']
因此,您必须在调用中使用命名空间管理器来选择元素,并且必须为每个元素指定命名空间的前缀:
var doc = XDocument.Load(_xmlName);
XElement root = doc.Root;
var namespaceManager = new XmlNamespaceManager(doc.CreateNavigator().NameTable);
namespaceManager.AddNamespace("hl7", "urn:hl7-org:v3");
//DEVO CERCARE DI SELEZIONARE LA SEZIONE GIUSTA
IEnumerable<XElement> lista = root.XPathSelectElements("hl7:component/hl7:structuredBody/hl7:component/hl7:section", namespaceManager);
foreach (XElement e in lista)
{
//to do
}
它不是格式正确的XML。你可以试试 这里 xml 验证器