Search code examples
c#xmlxpathxmldocument

C# XPath not selects some child nodes


I'm trying to use the XPath expression: .//*[@class='newsContent newsClosed']/b, but it is not working, always returns 0 elements.

I then tried to change the expression on .//*[@class='newsContent newsClosed'], and it's work.

Why first expression does not work?

I'm using XmlDocument.SelectSingleNode for retrieve elements.

Part of XHTML:

<div class="newsContent newsClosed">
    <b>some text that I need to take</b>
    <br />
    <p>
        text
    </p>
    <p>
        <b>text</b>
        <br />
        <b>text</b>
        <b>text</b>
    </p>
...

In FirePath both expressions are working properly.


Solution

  • Assuming this is XHTML, then you need to specify the namespace of your element: http://www.w3.org/1999/xhtml.

    var resolver = new XmlNamespaceManager(new NameTable());
    
    resolver.AddNamespace("html", "http://www.w3.org/1999/xhtml");
    
    var result = doc.SelectSingleNode(
        ".//*[@class='newsContent newsClosed']/html:b", resolver);
    

    My personal preference would be to ditch XPath altogether and use LINQ to XML:

    XNamespace html = "http://www.w3.org/1999/xhtml";
    
    var result = (string) doc.Descendants()
        .Where(element => (string) element.Attribute("class") == "newsContent newsClosed")
        .Elements(html + "b")
        .Single();
    

    See this fiddle for a demo.