How to get all children nodes with html agility pack and xpath

html-agility-pack xml xpath

Question

I have an XML document I am trying to parse with HTML Agility Pack using XPath. This is my code for getting the elements:

HtmlDocument htmldoc = new HtmlDocument();
htmldoc.Load(fileName);
HtmlNodeCollection nodes = htmldoc.DocumentNode.SelectNodes("feed/l");

This is my XML document

<?xml version="1.0" encoding="UTF-8"?>
<feed>
<tm>5701</tm>
<tp>1141</tp>
<pnr>1</pnr>
<l type="TEXT">
<ca>
<c id="21">JA</c>
</ca>
<pts>
<pt id="5">DO</pt>
</pts>
<od>MA 99</od>
<osd>2014-02-21</osd>
<od>2014-02-22</od>
<cu>url</cu>
<ip>ip</ip>
<ai>3184</ai>
<an>com</an>
<n id="1">US n</n>
</l>
</feed>

I am trying to get all the node that are children of l and their children, but my XPath returns nodes with l as a collection but the children are null. The nodes I am looking for grouped by l are these

 <ca>
<c id="21">JA</c>
</ca>
<pts>
<pt id="5">DO</pt>
</pts>
<od>MA 99</od>
<osd>2014-02-21</osd>
<od>2014-02-22</od>
<cu>url</cu>
<ip>ip</ip>
<ai>3184</ai>
<an>com</an>
<n id="1">US n</n>

but there are no children. Please help my XPath seems to be correct.

Popular Answer

Your XPath should be

feed/l/*

as you want the children element of l. So in your code it should be

HtmlNodeCollection nodes = htmldoc.DocumentNode.SelectNodes("feed/l/*");



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