Htmlagilitypack show node #text instead of span

c# html-agility-pack

Question

I have the following html markup :

        <div>
            <span id="391d0e73-d491-4e55-9ccb-b74c6923e070">This is a text
                element</span>
        </div>

Now if i save this div node in an HtmlNode object and after that when i access node's FirstChild then instead of giving span node as FirstChild it gives the node NAME: "#text" which is not present in the markup. Can please anybody help me out with this issue ?

Accepted Answer

In XML, nodes include elements, text, comments etc, e.g. in your document, the div can have 2 text children:

<div>
    text(1) Some text could be here
    <span id="391d0e73-d491-4e55-9ccb-b74c6923e070">This is a text
        element</span>
    text(2) More text could be here
</div>

You need to specify that you want the span child element on your HtmlNode, e.g.

divNode.SelectSingleNode("span")



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why