Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use XPath with XDocument?

There is a similar question, but it seems that the solution didn't work out in my case: Weirdness with XDocument, XPath and namespaces

Here is the XML I am working with:

<?xml version="1.0" encoding="utf-8"?>
<Report Id="ID1" Type="Demo Report" Created="2011-01-01T01:01:01+11:00" Culture="en" xmlns="http://demo.com/2011/demo-schema">
    <ReportInfo>
        <Name>Demo Report</Name>
        <CreatedBy>Unit Test</CreatedBy>
    </ReportInfo>
</Report>

And below is the code that I thought it should be working but it didn't...

XDocument xdoc = XDocument.Load(@"C:\SampleXML.xml");
XmlNamespaceManager xnm = new XmlNamespaceManager(new NameTable()); 
xnm.AddNamespace(String.Empty, "http://demo.com/2011/demo-schema");
Console.WriteLine(xdoc.XPathSelectElement("/Report/ReportInfo/Name", xnm) == null);

Does anyone have any ideas? Thanks.

like image 586
jojo Avatar asked Jun 02 '11 02:06

jojo


People also ask

How do I find the XPath of an XML node?

And you can later identify the node with an xpath query: newOrOldDocument. SelectSingleNode(string. Format("//*[contains(@some_id,'{0}')]", id));

What is XPath in C#?

XPath (XML Path Language) is based on a DOM representation of an XML document. Therefore, you can use XPath to find a specific node or nodes in an XML file that match some criteria defined in the XPath expression.

What is XPath expression in XML?

XPath is a syntax for defining parts of an XML document. XPath uses path expressions to navigate in XML documents. XPath contains a library of standard functions. XPath is a major element in XSLT and in XQuery. XPath is a W3C recommendation.

What is XPathSelectElement?

XPathSelectElement(XNode, String) Selects an XElement using a XPath expression. XPathSelectElement(XNode, String, IXmlNamespaceResolver) Selects an XElement using a XPath expression, resolving namespace prefixes using the specified IXmlNamespaceResolver.


2 Answers

If you have XDocument it is easier to use LINQ-to-XML:

var document = XDocument.Load(fileName);
var name = document.Descendants(XName.Get("Name", @"http://demo.com/2011/demo-schema")).First().Value;

If you are sure that XPath is the only solution you need:

using System.Xml.XPath;

var document = XDocument.Load(fileName);
var namespaceManager = new XmlNamespaceManager(new NameTable());
namespaceManager.AddNamespace("empty", "http://demo.com/2011/demo-schema");
var name = document.XPathSelectElement("/empty:Report/empty:ReportInfo/empty:Name", namespaceManager).Value;
like image 192
Alex Aza Avatar answered Sep 20 '22 15:09

Alex Aza


XPath 1.0, which is what MS implements, does not have the idea of a default namespace. So try this:

XDocument xdoc = XDocument.Load(@"C:\SampleXML.xml");
XmlNamespaceManager xnm = new XmlNamespaceManager(new NameTable()); 
xnm.AddNamespace("x", "http://demo.com/2011/demo-schema");
Console.WriteLine(xdoc.XPathSelectElement("/x:Report/x:ReportInfo/x:Name", xnm) == null);
like image 39
Richard Schneider Avatar answered Sep 20 '22 15:09

Richard Schneider