我的模特:
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 = ......
});
我该怎么做?
答案 0 :(得分:0)
如果我理解正确,您希望选择一组DateOfEvent
个包含属性Teams
,Championship
和DateOfEvent
的对象。
Teams
和Championship
来自多行,但有一个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"