Reputation: 185
I've table and td value as below code
foreach (var descendant in xmlDoc.Descendants("thead"))
{
var title = descendant.Element("td1 style=background:#cccccc").Value;
}
Assume I've below thead in the table
<thead>
<tr align="center" bgcolor="white">
<td1 style="background:#cccccc">Start</td1>
<td1 style="background:#cccccc">A</td1>
<td1 style="background:#cccccc">B</td1>
<td1 style="background:#cccccc">C</td1>
<td1 style="background:#cccccc">D</td1>
<td1 style="background:#cccccc">E</td1>
<td1 style="background:#cccccc">F</td1>
<td1 style="background:#cccccc">G</td1>
</tr>
</thead>
I need to get all td1 values
Upvotes: 0
Views: 91
Reputation: 236208
If you need td1
elements, then in this case you can select them directly:
var titles = xdoc.Descendants("td1").Select(td => (string)td);
Or you can use XPath
var titles = from td in xdoc.XPathSelectElements("//thread/tr/td1")
select (string)td;
NOTE if you are going to parse html documents, then better consider to use HtmlAgilityPack (available from NuGet).
Upvotes: 1
Reputation: 1499940
Your use of Element
is incorrect - you just pass in a name, not the whole content of an element declaration.
If you want all td1
elements, you want something like:
foreach (var descendant in xmlDoc.Descendants("thead"))
{
foreach (var title in descendant.Element("tr")
.Elements("td1")
.Select(td1 => td1.Value))
{
...
}
}
Or if you don't actually need anything from the thead
elements:
foreach (var title in descendant.Descendants("thead")
.Elements("tr")
.Elements("td1")
.Select(td1 => td1.Value))
{
...
}
(Do you really mean td1
rather than td
by the way?)
Upvotes: 2