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.

2
  • Why are you passing HTML from the controller to the View? You are breaking down all MVC pattern doing that! Commented Nov 11, 2013 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. Commented Nov 11, 2013 at 18:03

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.