Concat两个SelectNodes htmlagilitypack

时间:2016-04-24 21:55:47

标签: c# parsing html-agility-pack

我的模特:

public class Event
{
    public string DateOfEvent { get; set; }
    public string Teams { get; set; }
    public string Championship { get; set; }
}

DateOfEvent和Teams我使用以下代码从表中解析:

var matches = currentData.DocumentNode.SelectNodes("//tr[@class=' event-header']").
              Select(tr => new Event()
              {
                   DateOfEvent =
                    tr.SelectNodes("./td[1]/table/tr[1]/td[2]//text()[normalize-space()]")
                           .Single()
                           .InnerText.Trim(),
                   Teams = tr.SelectNodes("./td[1]/table/tr[1]/td[3]//text()[normalize-space()]")
                           .Single()
                           .InnerText.Trim(),
               });

我有很多关于DateOfEvent和Teams的记录。但我需要添加所有记录Field Championship,但其字段解析不是来自表。

var b = currentData.DocumentNode.SelectNode("//div[@class ='sport-champ']")
                                .Select(p=> new Event()
                  {
                      Championship = ......
                  });

我该怎么做?

1 个答案:

答案 0 :(得分:0)

如果我理解正确,您希望选择一组DateOfEvent个包含属性TeamsChampionshipDateOfEvent的对象。 TeamsChampionship来自多行,但有一个Championship,每个行都相同?

在这种情况下,首先得到var championship = currentData.DocumentNode.SelectNode("//div[@class ='sport-champ']") .InnerText.Trim();

Select

然后从行中执行var matches = currentData.DocumentNode.SelectNodes("//tr[@class=' event-header']"). Select(tr => new Event() { DateOfEvent = tr.SelectNodes("./td[1]/table/tr[1]/td[2]//text()[normalize-space()]") .Single() .InnerText.Trim(), Teams = tr.SelectNodes("./td[1]/table/tr[1]/td[3]//text()[normalize-space()]") .Single() .InnerText.Trim(), Championship = championship });

"The only words to count are Buttons and Shares for this text"