2016-04-24 89 views
1

我的模型:的毗連2的SelectNodes htmlagilitypack

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

DateOfEvent和我的團隊從表剖析與此代碼:

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和團隊多條記錄。但我需要添加到這些所有記錄Field Championship,但它的字段不是從表中解析的。

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

我該怎麼做?

+0

你怎麼每個錦標賽數據鏈接到DateOfEvent和團隊?按位置/索引還是有更好的規則,你可以從HTML中看到? – har07

回答

0

如果我理解正確,您希望選擇一組Event對象,其屬性爲DateOfEvent,TeamsChampionshipDateOfEventTeams來自多行,但有一個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 
    });