0

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.

Flood Gravemind
  • 3,773
  • 12
  • 47
  • 79
  • Why are you passing HTML from the controller to the View? You are breaking down all MVC pattern doing that! – Fals Nov 11 '13 at 18:01
  • @Fals I am not passing anystring from controller to View. The user is passing me a url from which I am getting the Html using System.Net.HttpWebRequest. Now I need certain values to provide to use from this Html. – Flood Gravemind Nov 11 '13 at 18:03

2 Answers2

5

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

carla
  • 1,970
  • 1
  • 31
  • 44
Andy T
  • 10,223
  • 5
  • 53
  • 95
1

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.

Fals
  • 6,813
  • 4
  • 23
  • 43