Skip to content Skip to sidebar Skip to footer

How Can I Parse Html String

I have a html string and i want to parse this;

Solution 1:

HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(html);

var teams = doc.DocumentNode.SelectNodes("//td[@width='313']")
                .Select(td => new TeamClass
                {
                    TeamName = td.Element("a").InnerText,
                    TeamId = HttpUtility.ParseQueryString(td.Element("a").Attributes["href"].Value)["ItemTypeID"]
                })
                .ToList();

Solution 2:

Have a look at this lib http:HTML Agility Pack.
It helps you with HTML parsing.


Solution 3:

You can use Regular expression

String html; //your html string
String pattern = @"action=ViewItemDetails&ItemType[I|i]D=(\d*)"">(.*)</a>";
MatchCollection matches = Regex.Matches(html, pattern);
var list = new List<TeamClass>();
foreach (Match match in matches)
{
    TeamClass team = new TeamClass();
    team.TeamName = match.Groups[2].Value;
    team.TeamId = Int32.Parse(match.Groups[1].Value);
    list.Add(team);
}

Solution 4:

Try Html Agility:

try something like (Untested Code):

var TeamList = from lnks in document.DocumentNode.Descendants()
               where lnks.Name == "a" && 
                    lnks.Attributes["href"] != null && 
                    lnks.InnerText.Trim().Length > 0
               select new
               {

                  TeamId= (lnks.Attributes["href"].Value).
                          Substring((lnks.Attributes["href"].Value).Length-1, 1),
                  TeamName= lnks.InnerText
               };

Post a Comment for "How Can I Parse Html String"