0

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>
6
  • 1
    try to paste the xml instead of image Commented May 23, 2020 at 10:15
  • 1
    Why not upload images of code on SO when asking a question? How others can help you without having a xml? By manual typing?:) Commented May 23, 2020 at 10:15
  • Can you give an example of atleast one such tag? I don't know which tags you're trying to get. More than one reference - like the city tag? Commented May 23, 2020 at 10:28
  • How to write queries on XML in namespaces Commented May 23, 2020 at 10:31
  • Post the text and we will form it. You need to put into a snippet box (<> )for code to stay formatted. Commented May 23, 2020 at 11:56

2 Answers 2

2

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);
         }
      }
   }
}
Sign up to request clarification or add additional context in comments.

Comments

1

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;

1 Comment

"I would like to read the value of the tag, which has more than one reference in the full XML"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.