c# - 从 C# 中的 Xml 字符串中获取元素
问题描述
我有一个字符串中的 xml 文档我想读取标签的值,它在完整的 XML 中有多个引用,所以我不能用其他方法唯一地读取它。我试过这段代码:
XDocument cdafile = XDocument.Parse(cXml)//The Xml code;
var myElement = cdafile.Elements("recordTarget").Elements("patientRole").Elements("patient").Elements("name").Elements("given");
但该值始终为空。也许我应该进入命名空间urn:hl7-org:v3,但我不知道怎么做。
这是xml代码。
<?xml version="1.0" encoding="UTF-8"?>
<ClinicalDocument xsi:schemaLocation="urn:hl7-org:v3 CDA.xsd"
xmlns="urn:hl7-org:v3" xmlns:mif="urn:hl7-org:v3/mif"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<recordTarget>
<patientRole classCode="PAT">
<id root="2.16.840.1.113883.2.9.4.3.2" extension="DC234RT566Y7"
assigningAuthorityName="LONDON" />
<addr use="HP">
<houseNumber>5</houseNumber>
<streetName>MY STREET NAME</streetName>
<city>LONDON</city>
<postalCode>12345</postalCode>
<county>GB</county>
</addr>
<patient>
<name>
<given>NAME</given>
<family>SURNAME</family>
</name>
<administrativeGenderCode codeSystem="2.16.840.1.113883.5.1" code="F" displayName="F" />
<birthTime value="20030215" />
<birthplace>
<place>
<addr>
<county>GB</county>
<city>LONDON</city>
<postalCode />
</addr>
</place>
</birthplace>
</patient>
</patientRole>
</recordTarget>
<author>
<time value="20200429110507" />
<assignedAuthor>
<id root="2.16.840.1.113883.2.9.4.3.2" extension="CF1234567890"
assigningAuthorityName="NYC" />
<id root="2.16.840.1.113883.2.9.2.160.4.2" extension="414681"
assigningAuthorityName="LONDON" />
<assignedPerson>
<name>
<prefix>DR.</prefix>
<given>DRNAME</given>
<family>DRSURNAME</family>
</name>
</assignedPerson>
</assignedAuthor>
</author>
</ClinicalDocument>
解决方案
您可以使用这个小控制台应用程序解析特定的 xml 文档:
using System;
using System.Linq;
using System.Xml.Linq;
namespace TestXml
{
class Program
{
static void Main(string[] args)
{
if (args.Length < 1) Console.WriteLine("You must enter a filename");
try
{
XDocument doc = XDocument.Load(args[0]);
var recTgts = doc.Root.Elements().Where(elm => elm.Name.LocalName == "recordTarget");
foreach(XElement recTgt in recTgts)
{
Console.WriteLine("Record Target");
Console.WriteLine("-------------");
var patientRoles = recTgt.Elements().Where(elm => elm.Name.LocalName == "patientRole");
foreach (XElement patientRole in patientRoles)
{
Console.WriteLine("Patient role " + patientRole.Attribute("classCode").Value);
Console.WriteLine("----------------");
var patients = patientRole.Elements().Where(elm => elm.Name.LocalName == "patient");
foreach (XElement patient in patients)
{
XElement name = patient.Elements().First(elm => elm.Name.LocalName == "name");
string given = name.Elements().First(elm => elm.Name.LocalName == "given").Value;
string family = name.Elements().First(elm => elm.Name.LocalName == "family").Value;
Console.WriteLine("Patient " + given + " " + family);
}
}
}
}
catch (Exception ex)
{
Console.WriteLine("Something went wrong: " + ex.Message);
}
}
}
}
推荐阅读
- java - 用什么代替 ArrayList 来分组数据?
- javascript - Gatsby 代码框:TypeError:无法读取未定义的属性“src”
- tensorflow2.0 - AttributeError:模块“张量流”没有属性“版本”
- sql-server - 将 SQL 结果写入 xlsx 的 Powershell 脚本 - 即使查询返回记录,Excel 工作表也是空白的
- docker - Docker - 守护进程上下文未指向本地目录
- javascript - “无服务器离线:找不到路由。” 在离线模式下运行 AWS Lambda 函数
- django - Webpack 使用文件夹名称而不是 Url 作为公共路径
- javascript - 不显示长度已过期值的记录
- android - React Native 0.60 - 无法使用 react-native run-android 运行应用程序:java.lang.NoClassDefFoundError
- material-ui - Material-UI Menu,MenuItem in a Table 不访问其记录的值,而是 MenuItem 只能访问最后一条记录的值