1

I have CDA XML I am trying to parse and query it using LINQ. However, I am unable to retrieve the values.

I am trying the following LINQ code:

var cdafile = XDocument.Load("cda.xml");

var patientCity = from c in cdafile.Elements("recordTarget")
                                   .Elements("patientRole")
                                   .Elements("addr")
                  select (string)c.Element("city").Value;

I am getting empty value in patientCity. Am I doing something wrong?

<?xml version="1.0" encoding="UTF-8"?>
<ClinicalDocument xmlns="urn:hl7-org:v3" xmlns:sdtc="urn:hl7-org:sdtc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:hl7-org:v3 http://xreg2.nist.gov:8080/hitspValidation/schema/cdar2c32/infrastructure/cda/C32_CDA.xsd">
  <realmCode code="US"/>
  <title>Cypress C32 Patient Test Record: Nelson Tuff</title>
  <recordTarget>
    <patientRole>
      <id root="Cypress" extension="4fe1ecbca9ffcc03cd0004e3"/>
      <addr use="HP">
        <streetAddressLine>202 Burlington Rd.</streetAddressLine>
        <city>Bedford</city>
        <state>MA</state>
        <postalCode>01730</postalCode>
      </addr>
      <telecom value="tel:+1-781-271-3000"/>
      <patient>
        <name>
          <given>George</given>
          <family>Mathew</family>
        </name>
      </patient>
    </patientRole>
  </recordTarget>
</ClinicalDocument>
1

1 Answer 1

2

You need to include default namespace in your query (urn:hl7-org:v3) and query Root property of XDocument:

XNamespace ns = "urn:hl7-org:v3";
var patientCity = 
    from c in cdafile.Root
        .Elements(ns + "recordTarget")
        .Elements(ns + "patientRole")
        .Elements(ns + "addr") 
    select (string)c.Element(ns + "city").Value;

This is because Elements searches direct children of node (which in case of XDocument would be document itself with its only direct children of ClinicalDocument).

You can use Descendants to search any descendants of current node, ignoring hierarchical walking altogether:

var patientCity = from c in cdafile.Descendants(ns + "city") select c.Value;
Sign up to request clarification or add additional context in comments.

Comments

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.