Reputation: 69
I have a xml document in a string I would like to read the value of the tag, which has more than one reference in the full XML, so I can't read it uniquely with other methods. I tried with this code:
XDocument cdafile = XDocument.Parse(cXml)//The Xml code;
var myElement = cdafile.Elements("recordTarget").Elements("patientRole").Elements("patient").Elements("name").Elements("given");
But the value is always empty. Maybe I should enter the namespace urn: hl7-org: v3, but I don't know how.
Here is the xml code.
<?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>
Upvotes: 0
Views: 402
Reputation: 411
You can parse you specific xml-document with this little console application:
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);
}
}
}
}
Upvotes: 2
Reputation: 7122
You need to use XNamespace like this:
XNamespace ns = "urn:hl7-org:v3";
var xml = XElement.Parse("<Z xmlns='urn:hl7-org:v3'><data>123</data></Z>");
var data = xml.Element(ns + "data");
var value = data.Value;
Upvotes: 1