Flood Gravemind
Flood Gravemind

Reputation: 3803

Reading String as HTML in c# and obtain Values of Elements by class

I have a string in c# MVC controller which is HTML.

eg: <html>...<head>...</head><body>...<div class="someClass">...</div><div class="someClass">...</div><div class="someClass">...</div></body></html>

Now I want to get all values of elements where Class = someClass and place them in a string array. Is this possible without using string manipulation functions? Currently I am using string manipulation like this

string str = above String;
if (str.Contains(@"<div class=""someClass"">"))
{
str = str.Remove(0, str.IndexOf(@"<div class=""someClass"">" + 22));
// add the text in array until </div> and move to next element

I am sure there is a way in c#. Can someone please guide me in the right direction.

Note: That the HTML string is not from File.

Note: This is not a Javascript question. Although I would love to use Javascript in Controller for this one.

Upvotes: 0

Views: 5586

Answers (2)

Andy T
Andy T

Reputation: 9881

I would recommend using the HtmlAgilityPack to parse the HTML string.

Here is an SO question similar to what you are asking:

Parsing HTML page with HtmlAgilityPack

Upvotes: 5

Fals
Fals

Reputation: 6839

You can manipulate HTML in C# using the HtmlDocument class.

FROM MSDN HtmlDocument Class:

Provides top-level programmatic access to an HTML document hosted by the WebBrowser control.

There's a exemple there. You can access and write html elements using this class.

Upvotes: 1

Related Questions