1

I have the following XML:

<Document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:hl7-org:v3 CDA_SDTC.xsd" xmlns="urn:hl7-org:v3" xmlns:cda="urn:hl7-org:v3" xmlns:sdtc="urn:hl7-org:sdtc">
<component>
<structuredBody>
  <component>
    <section>
      <templateId root="abs" />
      <title>A1</title>
      <text>
        <paragraph>Hello world!</paragraph>
      </text>
    </section>
  </component>
</structuredBody>
</component>
</Document>

I have the following code used to retrieve paragraph:

XDocument m_xmld = XDocument.Load(Server.MapPath("~/xml/a.xml"));
var m_nodelist = m_xmld.Descendants().Where(p => p.Name.LocalName == "section").
    Select(i => i.Element("text").Element("paragraph").Value).ToList();

Error:

Object reference not set to an instance of an object. 

However the following code works fine, but i want use above code.

XNamespace ns = "urn:hl7-org:v3";
var m_nodelist = m_xmld.Descendants(ns + "section").
       Select(i => i.Element(ns + "text").Element(ns + "paragraph").Value).ToList();
4

4 に答える 4

1

これを試して

var m_nodelist = m_xmld.Root.Descendants("rows")

ノードを選択するときに名前空間を指定したい場合は、試すことができます

var m_nodelist = m_xmld.Root.Descendants(XName.Get("rows", "urn:hl7-org:v3"))
于 2013-09-16T17:01:54.500 に答える
1
var m_nodelist = m_xmld.Descendants().Where(p => p.Name.LocalName == "rows").
Select(u => u.Attribute("fname").Value).ToList();

アップデート :

var m_nodelist = m_xmld.Descendants().Where(p => p.Name.LocalName == "section").
Select(u => (string)u.Descendants().FirstOrDefault(p => p.Name.LocalName == "paragraph")).ToList();
于 2013-09-16T17:02:43.217 に答える
1

xmlns="urn:hl7-org:v3"はデフォルトの名前空間であるため、参照する必要があります...

XNamespace ns="urn:hl7-org:v3";
m_xmld.Descendants(ns+"rows")....

また

名前空間自体を避けることができます

m_xmld.Elements().Where(e => e.Name.LocalName == "rows")
于 2013-09-16T17:00:05.697 に答える
0

namespace正しく識別したように、ノード ルックアップにを追加する必要があります。

XNamespace nsSys = "urn:hl7-org:v3";
var m_nodelist = m_xmld.Descendants(nsSys + "rows").Select(x => x.Attribute("fname").Value).ToList();
于 2013-09-16T17:06:00.740 に答える