Zain Shaikh
Zain Shaikh

Reputation: 6043

getting text from html document using HtmlAgilityPack via XPath

I have following html in a file, I am loading this file into an HTMLDocument using HtmlAgilityPack.

The problem is that I only want to get Hello World! using XPath and not the inner text.

How do I achieve this?

<ul>
    <li>
        Hello world!
        <ul>
            <li>
                Welcome to planet!
            </li>
        </ul>
    </li>
</ul>

Upvotes: 4

Views: 2588

Answers (2)

dash
dash

Reputation: 91462

The XPath:

//ul/li[1]/text()

Should select the actual text "Hello World!"

You can then select the value of this node.

In use:

string text = doc.DocumentElement.SelectSingleNode("//ul/li[1]/text()").Value;

In essence, what this says is navigate to a ul node, select the first li, and then select the text() node.

Upvotes: 2

pil0t
pil0t

Reputation: 2185

htmlDocument.DocumentNode.SelectNodes("//ul/li").First().FirstChild.InnerText;

will return Hello world!

Upvotes: 3

Related Questions