From this XHTML source:
<div class = "page">
<h1>UNIQUE NAME</h1>
<table>
<tbody>
<tr>
<td>DATA TO EXTRACT 1</td>
</tr>
<tr>
<td />
<td />
<td />
<td />
<td />
<td>DATA TO EXTRACT 2</td>
</tr>
</tbody>
</table>
etc...
There are multiple instances of UNIQUE NAME with a similar set of child elements.
I need to locate the UNIQUE NAME element and extract all values (DATA TO EXTRACT) within each of the child element tags. In addition, I need to keep a count of where each value is located. For example DATA TO EXTRACT 1 would be at tr 1, td 1. DATA TO EXTRACT 2 would be at tr 2, td 6.
I am new to linq to xml and I was wondering whether someone could point me in the right direction with regards to a strategy. I have managed to figure out how to get to the UNIQUE name element with the following code:
var choice1 = (from category in _data.Descendants("div")
where category.Element("h1").Value == "UNIQUE NAME"
select category).DescendantNodes();
This returns a set of the values, which I’m sure I could loop through but I’m sure there must be a more elegant way of achieving this goal.
Many thanks!
Here’s one way of doing it using LINQ:
An example of how to use your results:
…which would output: