0

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.

1
  • 1
    Provide a minimal reproducible example. You claim this is XHTML, so the issue is most likely that you haven't specified the namespace for b. Commented Jul 29, 2016 at 13:22

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.