Googler
Googler

Reputation: 535

Absolute Xpath to get list of childnodes?

AT 380509 T 20071215

For the above xml file, i need the xpath to receive the childnodes below it:

Output i need is :

<exchange-documents xmlns="http://www.epo.org/exchange">
    <exchange-document country="AT" doc-number="380509" family-id="38826527" kind="T" system="ops.epo.org">
      <bibliographic-data>
        <publication-reference data-format="docdb">
          <document-id>
            <country>AT</country>
            <doc-number>380509</doc-number>
            <kind>T</kind>
            <date>20071215</date>
          </document-id>
        </publication-reference>  
        <parties>
          <applicants>
          </applicants>
          <inventors>
          </inventors>
        </parties>
      </bibliographic-data>
    </exchange-document>

I using Linq-Xml to get the following data:

This is my Xpath and code:

var list = doc1.XPathSelectElement("exchange-document");

I couldnt retreive the needed output.It returns null for the above code. Can anyone pls help on this by providing the correct xpath to retieve the child nodes. Else is there any other way to retrieve it.

Upvotes: 1

Views: 593

Answers (3)

St&#233;phane
St&#233;phane

Reputation: 11854

the problem is well explained here : Search XDocument using LINQ without knowing the namespace

Your xml has namespaces. When you search for an element the Name attribute is an XNamae which include the namespace. So you have to look for Name.LocalName == [theNameOfYourNode]

var xml = XElement.Parse(@"<worldpatentdata xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema"">
          <meta name=""elapsed-time"" value=""329"" xmlns=""http://ops.epo.org/\""/>
          <exchange-documents xmlns=""http://www.epo.org/exchange\"">
            <exchange-document country=""AT"" doc-number=""380509"" family-id=""38826527"" kind=""T"" system=""ops.epo.org"">
              <bibliographic-data>
                <publication-reference data-format=""docdb"">
                  <document-id>
                    <country>AT</country>
                    <doc-number>380509</doc-number>
                    <kind>T</kind>
                    <date>20071215</date>
                  </document-id>
                </publication-reference>  
                <parties>
                  <applicants>
                  </applicants>
                  <inventors>
                  </inventors>
                </parties>
              </bibliographic-data>
            </exchange-document>
          </exchange-documents>
        </worldpatentdata>");

        var a = xml.Descendants().First(x => x.Name.LocalName == "exchange-documents");
        Console.WriteLine(a);

Upvotes: 1

Chris Schmich
Chris Schmich

Reputation: 29466

Your XML document uses XML namespaces, so you need to specify them in your XPath expression. See the following for how to do this:

Upvotes: 1

Kangkan
Kangkan

Reputation: 15571

Put the complete path for the xpath to retrieve.

Upvotes: 0

Related Questions