Skip to content Skip to sidebar Skip to footer

Parsing Html Using Agility Pack

I have a html to parse(see below)

InBox

Solution 1:

You can write the following code:

HtmlDocumentdoc=newHtmlDocument();
doc.LoadHtml(responseFromServer);

foreach (HtmlNode link in doc.DocumentNode.SelectNodes("//tr[@onclick]"))
{
    HtmlAttributeatt= link.Attributes["onclick"];
    ClassMailBoxclassMailbox=newClassMailBox() { LinkToMail = att.Value };
    classMailBoxes.Add(classMailbox);
}

intcurrentPosition=0;

foreach (HtmlNode tableDef in doc.DocumentNode.SelectNodes("//tr[@onclick]/td[1]"))
{
    classMailBoxes[currentPosition].From = tableDef.InnerText;
    currentPosition++;
}

To keep this code simple, I'm assuming some things:

  1. The email is always on the first td inside the tr which contains an onlink property
  2. Every tr with an onlink attribute contains an email

If those conditions don't apply this code won't work and it could throw some exceptions (IndexOutOfRangeExceptions) or it could match links with wrong email addresses.

Post a Comment for "Parsing Html Using Agility Pack"