Parsing HTML with c#.net

C#HtmlWindows Phone-7

C# Problem Overview


I'm trying to parse the following HTML file, I'd like the get the value of key. This is being done on Silverlight for Windows phone.

<HTML>
<link ref="shortcut icon" href="favicon.ico">
<BODY>
<script Language="JavaScript">
location.href="login.html?key=UEFu1EIsgGTgAV7guTRhsgrTQU28TImSZkYhPMLj7BChpBkvlCO11aJU2Alj4jc5"
</script>
<CENTER><a href="login.html?key=UEFu1EIsgGTgAV7guTRhsgrTQU28TImSZkYhPMLj7BChpBkvlCO11aJU2Alj4jc5">Welcome</a></CENTER></BODY></HTML>

any idea's on where to go from here?

thanks

C# Solutions


Solution 1 - C#

Give the HTMLAgilityPack a look into. Its a pretty decent HTML parser

http://html-agility-pack.net/?z=codeplex

Here's some code to get you started (requires error checking)

HtmlDocument document = new HtmlDocument(); 
string htmlString = "<html>blabla</html>";
document.LoadHtml(htmlString);
HtmlNodeCollection collection = document.DocumentNode.SelectNodes("//a");
foreach (HtmlNode link in collection)
{
     string target = link.Attributes["href"].Value;
}

Solution 2 - C#

You can use regular expression (Regex class) for it. The expression can be something like that: login.html\?key=[^"]*

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNathanView Question on Stackoverflow
Solution 1 - C#KurruView Answer on Stackoverflow
Solution 2 - C#Rafal SpacjerView Answer on Stackoverflow