tags:

views:

83

answers:

3

Hi this my xml file,

<?xml version="1.0"?>
<worldpatentdata xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
  <meta name="elapsed-time" value="329" xmlns="http://ops.epo.org"/&gt;
  <exchange-documents xmlns="http://www.epo.org/exchange"&gt;
    <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>

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"&gt;
    <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.

A: 

Put the complete path for the xpath to retrieve.

Kangkan
Like this "worldpatentdata/exchange-documents/exchange-document" ?Still am receiving null response
Googler
You need to put the expression like this: "/worldpatentdata/exchange-documents/exchange-document".
Kangkan
+1  A: 

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
+1  A: 

the problem is well explained here : http://stackoverflow.com/questions/2610947/search-xdocument-with-linq-with-out-knowing-the-namespace/2611152#2611152

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""&gt;
          <meta name=""elapsed-time"" value=""329"" xmlns=""http://ops.epo.org/\""/&gt;
          <exchange-documents xmlns=""http://www.epo.org/exchange\""&gt;
            <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);
Stephane
thanks stephane.. it works
Googler
great! :) XLinq is really nice to work with XML, many prefer it to XPath.
Stephane