1
votes

My model:

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

DateOfEvent and Teams I parse from table with this code:

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(),
               });

I have many records with DateOfEvent and Teams. But I need to add to these all records Field Championship, but Its field parse not from table.

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

How can I do that?

1
How you link each Championship data to DateOfEvent and Teams? By position/index or there is a better rule that you can see from the HTML? - har07

1 Answers

0
votes

If I understand correctly, you want to select a set of Eventobjects with properties DateOfEvent, Teams, and Championship. DateOfEvent and Teams come from multiple rows, but there's one Championship and it will be the same for each?

In that case, just get Championship first:

var championship = currentData.DocumentNode.SelectNode("//div[@class ='sport-champ']")
    .InnerText.Trim();

and then do your Select from the rows:

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
     });