Search code examples
c#htmlxpathhtml-agility-pack

XPath : How to get concatenated text of two sibling nodes?


I would like to know how to get concatenated text of two sibling nodes.

This is my code.

string html =
    "<html>" +
    "   <div class='abc'>" +
    "       <h3><a href='def'>ghi</a></h3>" +
    "       <div>text1</div>" +
    "       <div>text2</div>" +
    "   </div>" +
    "   <div class='abc'>" +
    "       <h3><a href='jkl'>mno</a></h3>" +
    "       <div>text3</div>" +
    "       <div>text4</div>" +
    "   </div>" +
    "</html>";
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(html);
HtmlNodeCollection nodes = doc.DocumentNode.SelectNodes("//div[@class='abc']");
HtmlNodeCollection nodes2, nodes3;
foreach (HtmlNode node in nodes)
{
    nodes2 = node.SelectNodes(".//h3/a");
    nodes3 = node.SelectNodes("?????????????");
}

I want to get result

text1text2

and then

text3text4

How do I write a query replace of question marks? I know I can get text with iterating through nodes using foreach. But I must do this with XPath query.

Thanks.


Solution

  • @Mathias Müller 's comment is answer.

    SelectNodes returns node list and we must navigate through collection nodes with C# programming.

    The mistake is that I'm waiting text result.