1

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.

Googler
  • 525
  • 3
  • 12
  • 30

3 Answers3

1

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

Chris Schmich
  • 29,128
  • 5
  • 77
  • 94
1

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);
Community
  • 1
  • 1
Stéphane
  • 11,755
  • 7
  • 49
  • 63
0

Put the complete path for the xpath to retrieve.

Kangkan
  • 15,267
  • 10
  • 70
  • 113
  • Like this "worldpatentdata/exchange-documents/exchange-document" ? Still am receiving null response – Googler May 05 '10 at 07:47
  • You need to put the expression like this: "/worldpatentdata/exchange-documents/exchange-document". – Kangkan May 05 '10 at 09:01