Html Agility Pack 合并查询
我有一个表:
...some td's with not needed links
<td>1010</td>
<td>Building</td>
<td>Adress stree 55</td>
<td>00000 City</td>
<td>
<a href="http://www.adress.xy/file.kml" target="_self">
<img align="top" border="1" src="/custom/img/kml.gif" alt="Details" title="Details" />
</a>
</td>
我使用此查询来获取内部文本信息:
HtmlDocumet doc = new HtmlDocument();
doc.LoadHtml(html);
var node = doc.DocumentNode.Descendants("table")
.FirstOrDefault(x => x.Attributes["style"].Value == "table-layout:auto")
.Elements("tr")
.Select(tr => tr.Elements("td").Select(td => td.InnerText).ToArray)).ToArray();
但我还想向数组添加一个带有 .kml 链接的 url。 所以问题是:如何合并查询以获得内部文本和 kml 链接?
该查询的结果是:
string[i][j]
其中 i= tr- 元素的数量,j - td- 元素的数量
示例:
string[0][0]="1010"
string[0][1]="Building"
我还希望有: string[i][4] = "http://www. adress.xy/file.kml"
PS 整张桌子都在这里。
I have a table kind of:
...some td's with not needed links
<td>1010</td>
<td>Building</td>
<td>Adress stree 55</td>
<td>00000 City</td>
<td>
<a href="http://www.adress.xy/file.kml" target="_self">
<img align="top" border="1" src="/custom/img/kml.gif" alt="Details" title="Details" />
</a>
</td>
I use this query to get the innertext information:
HtmlDocumet doc = new HtmlDocument();
doc.LoadHtml(html);
var node = doc.DocumentNode.Descendants("table")
.FirstOrDefault(x => x.Attributes["style"].Value == "table-layout:auto")
.Elements("tr")
.Select(tr => tr.Elements("td").Select(td => td.InnerText).ToArray)).ToArray();
but I would also like to add to the array an url with .kml links.
So the question is: how is it possible to merge querys to get innertext and the kml link?
the result of this query is:
string[i][j]
where i= number of tr- elements and j - number of td- elements
Example:
string[0][0]="1010"
string[0][1]="Building"
I would like also to have: string[i][4] = "http://www.adress.xy/file.kml"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不担心获取数组的数组,如果你得到列表会更好。
我会考虑创建一个匿名类型来表示您的行,这样您就可以为您的列提供更有用的名称。也许甚至可以将结果放入
DataTable
中。以防万一您因某种原因无法使用 xpath(或者您想知道等效的 LINQ 查询),您可以将使用 xpath 的行替换为:
I wouldn't worry about getting arrays of arrays, it would be better if you got lists instead.
I would consider making an anonymous type to represent your rows that way you could give more useful names you your columns. Perhaps even put the results in a
DataTable
instead.Just in case you won't be able to use xpath for whatever reason (or you wanted to know the equivalent LINQ queries), you could replace the line that uses the xpath with this: