如何从html敏捷性获得标题,描述?

时间:2013-05-27 05:21:56

标签: c# html windows-phone-7 html-agility-pack

我想从网上获得标题,描述。我用

var title= (from item in doc.DocumentNode.SelectNodes(
     "//div[@class='section sectionMain recentNews']//a[@class='newsTitle']")
     select item).ToList();

var des= (from item in doc.DocumentNode.SelectNodes(
     "//div[@class='section sectionMain recentNews']//div[@class='newsText']") 
     select item).ToList();

现在,我想使用查询选择所有标题,描述如:

var data = (from item in doc.DocumentNode.SelectNodes(
"//div[@class='section sectionMain recentNews']") select new 
{
    title =..........., description =.......
}

1 个答案:

答案 0 :(得分:0)

如果您不想在同一个循环中执行此操作,可以使用的方法之一是组合两个列表:

 var title= (from item in doc.DocumentNode.SelectNodes(
 "//div[@class='section sectionMain recentNews']//a[@class='newsTitle']")
 select item).ToList();

var des= (from item in doc.DocumentNode.SelectNodes(
     "//div[@class='section sectionMain recentNews']//div[@class='newsText']") 
     select item).ToList();

var items = title.Zip(des, (t, d) => new {Title = t, Description = v })

希望这会有所帮助:)