如何在c#中使用htmlagilitypack访问子节点【基于'from'条款]
我有一个 html :
<div class="article-listing">
<div class="media-data">
<h4><a href="http://thenextweb.com/media/2012/01/05/24symbols-white-labels-its-platform-to-give-any-organization-its-own-spotify-for-books/">24symbols White-Labels its 'Spotify for Books'</a></h4>
<p class="article-meta"><a href="http://thenextweb.com/media/">TNW Media</a> • <a href="http://thenextweb.com/author/martin/" title="Posts by Martin Bryant" rel="author">Martin Bryant</a> • <span class="date" title="1325781355">January 5, 2012</span></a></p>
<p>24symbols, the ‘Spotify for books’ startup that launched last summer, has been busy developing its service that allows users instant access to a library of books for a fixed fee….</p>
</div></div>
I am using this xpath code for extracting required information :
var webGet = new HtmlWeb();
var document = webGet.Load(page);
var infos = from info in document.DocumentNode.SelectNodes("//div[@class='article-listing']//div[@class='media-data']")
from link in info.Descendants("a").Where(x => x.Attributes.Contains("href"))
from content in info.Descendants("p").Where(y => y.HasAttributes != true)
from author in info.Descendants("//p[@class='article-meta']//a[@rel='author']").Where(z => z.Attributes.Contains("href"))
from date in info.Descendants("//p[@class='article-meta']//span")
select new
{
LinkURL = link.Attributes["href"].Value,
Text = content.InnerText,
Author = author.InnerText,
Date = date.InnerText
};
lvLinks.DataSource = infos;
lvLinks.DataBind();
我正在使用列表视图控件在 asp 页面中显示数据,如使用
但它不起作用...它没有显示任何错误,也没有在页面上显示任何数据。
使用不同的“from”子句选择节点可能存在一些问题。
请推荐我 谢谢
i have a html as :
<div class="article-listing">
<div class="media-data">
<h4><a href="http://thenextweb.com/media/2012/01/05/24symbols-white-labels-its-platform-to-give-any-organization-its-own-spotify-for-books/">24symbols White-Labels its 'Spotify for Books'</a></h4>
<p class="article-meta"><a href="http://thenextweb.com/media/">TNW Media</a> • <a href="http://thenextweb.com/author/martin/" title="Posts by Martin Bryant" rel="author">Martin Bryant</a> • <span class="date" title="1325781355">January 5, 2012</span></a></p>
<p>24symbols, the ‘Spotify for books’ startup that launched last summer, has been busy developing its service that allows users instant access to a library of books for a fixed fee….</p>
</div></div>
I am using this xpath code for extracting required information :
var webGet = new HtmlWeb();
var document = webGet.Load(page);
var infos = from info in document.DocumentNode.SelectNodes("//div[@class='article-listing']//div[@class='media-data']")
from link in info.Descendants("a").Where(x => x.Attributes.Contains("href"))
from content in info.Descendants("p").Where(y => y.HasAttributes != true)
from author in info.Descendants("//p[@class='article-meta']//a[@rel='author']").Where(z => z.Attributes.Contains("href"))
from date in info.Descendants("//p[@class='article-meta']//span")
select new
{
LinkURL = link.Attributes["href"].Value,
Text = content.InnerText,
Author = author.InnerText,
Date = date.InnerText
};
lvLinks.DataSource = infos;
lvLinks.DataBind();
I am using list view control to show data in asp page as using <li> <%# Eval("LinkURL") %> - <%# Eval("Text") %> - <%# Eval("Author") %> </li>
But its not working...It is not showing any errors and nor it shows any data on the page..
May be there is some problem with selecting nodes using different 'from' clauses.
Please suggest me
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我已经解决了这个问题......这是与节点的选择有关。需要使用 SelectNodes 而不是后代,我还需要修复 SelectNodes() 方法中传递的标签。
我们可以使用下面的代码,它会工作得很好。
lvLinks.DataSource = 信息;
lvLinks.DataBind();
工作正常...现在没问题
I have resolved the issue... It was with selection of nodes. One need to use SelectNodes instead of Descendants and also i need to fix the tags passed in SelectNodes() method.
We can use following code and it would work fine.
lvLinks.DataSource = infos;
lvLinks.DataBind();
Its working fine...No issue now