开发者

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>
        开发者_如何学C    <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.


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);


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

  • XPathSelectElement
  • How to: Find Elements in a Namespace (XPath-LINQ to XML)


Put the complete path for the xpath to retrieve.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜