简体   繁体   中英

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>

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM