开发者

Retrieve XPath of an XML element by using its value

开发者 https://www.devze.com 2022-12-24 23:22 出处:网络
My XmlFile looks like this: <?xml version=\"1.0\"?> <document-inquiry> <publication-reference data-format=\"docdb\" xmlns=\"http://www.epo.org/exchange\">

My XmlFile looks like this:

<?xml version="1.0"?> <document-inquiry> <publication-reference data-format="docdb" xmlns="http://www.epo.org/exchange"> <document-id> <country>EP</country> <doc-number>2160088</doc-number> <kind>A1</kind> </document-id> </publication-reference>

</document-inquiry>

For the above xml i need to get the xpath of a specific elem开发者_运维知识库ent say for example "country element" as

My Output: "/document-inquiry/publication-reference/document-id/country"

My Input : By using its value "EP"

This is the code i tried

doc.SelectSingleNode("/document-inquiry/publication-reference/document-id[text()='EP']");

I receivev null for the above code.

I have to get it using the c# code. Can anyone pls help me on this


using System;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
using System.Xml.XPath;

class Program
{
    static void Main()
    {
        var doc = XDocument.Load("D:\\xml\\neo.xml");
        var ns = new XmlNamespaceManager(new NameTable());
        ns.AddNamespace("ns", "http://www.epo.org/exchange");
        var elem = XDocument.Load("D:\\xml\\neo.xml")
            .XPathSelectElement("//ns:document-id[ns:doc-number='1000']", ns);
        if (elem != null)
        {
            Console.WriteLine(elem.ToString());
            Console.ReadLine();
        }
    }
}

This works perfectly for me.

0

精彩评论

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

关注公众号