user1649941
user1649941

Reputation: 123

Parsing Complex XML using LINQ

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>

Upvotes: 1

Views: 1291

Answers (1)

k.m
k.m

Reputation: 31454

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;

Upvotes: 2

Related Questions