LCJ
LCJ

Reputation: 22661

Reading XML Enumeration yielded no results

I have following C# code to read values from XML file. But when I expand the result, it says “Enumeration yielded no results”

Questions

  1. What is the issue here?
  2. How to fix this?
  3. How to read the value of "family" element

Note: I have seen similar questions in stack overflow and other forums and tried the recommendations already.

C#

        XElement doc = XElement.Load(@"Test.xml");
        XNamespace ns = "urn:hl7-org:v3";
        IEnumerable<XElement> childList = from el in doc.Elements() select el;

        IEnumerable<XElement> result = childList.Elements(ns+ "assignedPerson");

XML

<?xml version="1.0" encoding="UTF-8"?>
<ClinicalDocument xmlns="urn:hl7-org:v3" xmlns:sdtc="urn:hl7-org:sdtc" xmlns:xs="http://www.w3.org/2001/XMLSchema"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:fn="http://www.w3.org/2005/xpath-functions" >
  <realmCode code="US"/>
  <typeId root="2.16.840.1.1" extension="POCD_HD000040"/>
  <languageCode code="en-US"/>
  <author>
    <time value="334455"/>
    <assignedAuthor>
      <id root="2.16.840.1.113883" extension="771544"/>
      <code codeSystem="2.16.840.1" codeSystemName="Provider Codes" code="207R00000X" displayName="Nephrology"/>
      <addr use="WP">
        <city>East Point</city>
      </addr>
      <telecom use="WP" value="tel:(xxx)xxx-xxxx"/>
      <assignedPerson>
        <name>
          <prefix>Mrs</prefix>
          <given>Test</given>
          <family>Martin</family>
          <suffix>NP</suffix>
        </name>
      </assignedPerson>
    </assignedAuthor>
  </author>
</ClinicalDocument>

Upvotes: 0

Views: 508

Answers (1)

Enigmativity
Enigmativity

Reputation: 117144

There is no assignedPerson that is a child of the doc.Elements() collection.

Try this instead:

    XNamespace ns = "urn:hl7-org:v3";
    IEnumerable<XElement> result =
        doc
            .Descendants(ns + "assignedPerson")
            .Descendants(ns + "family");

Upvotes: 4

Related Questions