views:

28

answers:

1

My XmlFile looks like this:

<?xml version="1.0"?> <document-inquiry> <publication-reference data-format="docdb" xmlns="http://www.epo.org/exchange"&gt; <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 element 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

A: 

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.

Googler