Luca
Luca

Reputation: 453

Extract entire table using Htmlagilitypack and Linq-XML

I've found a code snippet from a post on here a while back. As I'm a beginner with C# I'm kind of lost.

I'm trying extract all cells from a table and write them to an XML file that looks like this

<?xml version="1.0" encoding="utf-8"?>
<Stats Date="11/4/2013">
  <Player Rank="1">
    <Name>P.K. Subban</Name>
    <Team>MTL</Team>
    <Pos>D</Pos>
    <GP>15</GP>
    <G>3</G>
    <A>11</A>
    <Pts>14</Pts>
    <PlusMinus>+2</PlusMinus>
    <PIM>16</PIM>
    <PP>2</PP>
    <SH>0</SH>
    <GW>0</GW>
    <OT>0</OT>
    <Shots>47</Shots>
    <ShotPctg>6.4</ShotPctg>
    <TOIPerGame>24:29</TOIPerGame>
    <ShiftsPerGame>27.3</ShiftsPerGame>
    <FOWinPctg>0.0</FOWinPctg>
  </Player>
</Stats>

My issue is I don't know how to loop through the entire table which is 25 rows and 19 columns. I'm only able to extract 1 row out of the whole table.

This is what I have (I've taking the snippet and modified the elementNames and Xpath

public void ParseHtml()
        {
            var htmlDoc = new HtmlDocument();
            htmlDoc.LoadHtml(Source);


            var cells = htmlDoc.DocumentNode
                                                   .SelectNodes("//table[@class='data stats']/tbody/tr/td")
                                                   .Select(node => node.InnerText.Trim())
                                                   .ToList();

            var elementNames = new[] { "Name", "Team", "Pos", "GP", "G", "A", "Pts", "PlusMinus", "PIM", "PP", "SH", "GW", "OT", "Shots", "ShotPctg", "TOIPerGame", "ShiftsPerGame", "FOWinPctg" };
            var xmlDoc = new XElement("Stats", new XAttribute("Date", DateTime.Now.ToShortDateString()),
                    new XElement("Player", new XAttribute("Rank", cells.First()),
                        cells.Skip(1)
                             .Zip(elementNames, (Value, Name) => new XElement(Name, Value))
                             .Where(element => !String.IsNullOrEmpty(element.Value))
                    )
                );
            xmlDoc.Save("parsed.xml");
        }

Things I've tried: changing

var cells = htmlDoc.DocumentNode
.SelectNodes("//table[@class='data stats']/tbody/tr/td")
.Select(node => node.InnerText.Trim())
.ToList();

To

foreach (HtmlNode cells in htmlDoc.DocumentNode
    .SelectNodes("//table[@class='data stats']/tbody/tr/td")
    .Select(node => node.InnerText.Trim())
    .ToList() )
{
var elementNames....
..
...

With this change I get no values and the xml nodes are reduced to 2. Can anyone help me out? I've been trying for 3 days to solve this.

Edit: HTML source file: http://www.nhl.com/ice/playerstats.htm?season=20132014&gameType=2&team=BUF&position=S&country=&status=&viewName=summary

Upvotes: 0

Views: 567

Answers (1)

Alex Filipovici
Alex Filipovici

Reputation: 32571

Try this:

// ...
var xmlDoc = new XElement("Stats",
    new XAttribute("Date", DateTime.Now.ToShortDateString()));
XElement iteratingElement = null;
var length = elementNames.Length + 1;
for (int i = 0; i < cells.Count; i++)
{
    if (i % ((i == 0) ? 1 : length) == 0)
    {
        iteratingElement = new XElement("Player",
            new XAttribute("Rank", cells[i]));
        xmlDoc.Add(iteratingElement);
    }
    else
    {
        iteratingElement
            .Add(new XElement(elementNames[(i % length) - 1], cells[i]));
    }
}
xmlDoc.Save("parsed.xml");

Upvotes: 1

Related Questions