[英]How can access to element or node to xml file from c#
我有这个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”每次都是空的。 错误在哪里?
问题出在名称空间上。 根下的所有元素均取自与根上标记的名称空间相同的名称空间。
如果您已经安装了Visual Studio的XPathInfo,然后从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']
因此,必须在调用中使用NamespaceManager来选择元素,并且必须为每个元素的名称空间指定前缀:
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验证器
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.