XPath - how do you select node children?

I have an XmlDocument containing an XHTML table. I would like to skip it to process the table cells one row at a time, but the code below returns all the cells in a nested loop instead of the ones for the current row:

XmlNodeList tableRows = xdoc.SelectNodes("//tr"); foreach (XmlElement tableRow in tableRows) { XmlNodeList tableCells = tableRow.SelectNodes("//td"); foreach (XmlElement tableCell in tableCells) { // this loops through all the table cells in the XmlDocument, // instead of just the table cells in the current row } } 

What am I doing wrong? Thanks

+8
c # xpath
source share
1 answer

Run the inner path with "." to indicate that you want to start with the current node. The initial "/" is always looking from the root of an xml document, even if you specify it in a sub-tray.

So:

 XmlNodeList tableCells = tableRow.SelectNodes(".//td"); 

or even

 XmlNodeList tableCells = tableRow.SelectNodes("./td"); 

as those <td> are probably directly below this <tr> .

+14
source share

All Articles