Take the 2-minute tour ×

There are many times I need to extract value of an element from a HTML page. Something like this:

<!-- many html here -->
<input type="hidden" name="id" value="ExtractMe!">
<!-- many html here -->

How can extract the value easily?

share|improve this question

4 Answers 4

up vote 4 down vote accepted

Have a look at the HTMLAgility pack, it makes this type of task very easy and regex-free.

share|improve this answer
Is there a faster solution? currently I use indexOf. –  Xaqron May 10 '11 at 14:37
@Xaqron regex or your own parser are the two fastest options. If you have XHTML you could try LINQ-to-XML –  Chris S May 10 '11 at 16:31

Why don't you use regular expressions? This the MSDN Regular Expression Documentation, in there you can look for The section Extracting a Single Match or the First Match.

share|improve this answer
Regex eats CPU and implementation is not easy. –  Xaqron May 10 '11 at 14:45
"Regex eats CPU" - so are you planning on running this 100,000s of times? Is performance a factor? –  Lee Gunn May 10 '11 at 14:53
Implementation of Regex in .NET is fairly easy, the classes are there, perhaps writing the right pattern expression is trickier but there are tools (and stackoverflow) to help you with that. As to the performance part, @Lee Gunn asked the right questions. –  PedroC88 May 10 '11 at 14:58

If you just want to pluck values you're probably best to parse this as XML. You have a choice of standard XML or LINQ.

have a look here or here for some examples.

share|improve this answer
But in most cases HTML is not a valid XML document and will fail. –  DanielB May 10 '11 at 14:34

If you need to parse HTML within your C# application consider using HTMLAgilityPack from here http://htmlagilitypack.codeplex.com/

share|improve this answer

Your Answer


