1

Possible Duplicate:
How to use HTML Agility pack

I have html code below:

<div><span class="help">This is text.</span>Hello, this is text.</div> <div>I have a question.<span class="help">Hi</span></div> 

Now, I want to remove text which is between <span class="help"></span> using C#. So, I want to leave only

<div>Hello, this is text.</div> <div>I have a question.</div> 

Anyone has any idea?

0

4 Answers 4

6

You should use Html Agility Pack to work with html.

string text = @"<div><span class=""help"">This is text.</span>Hello, this is text. </div> <div>I have a question.<span class=""help"">Hi</span></div>"; HtmlDocument doc = new HtmlDocument(); doc.LoadHtml(text); var nodes = doc.DocumentNode.SelectNodes("//span[@class='help']"); foreach( HtmlNode node in nodes) { node.Remove(); } String result = doc.DocumentNode.InnerHtml; 
Sign up to request clarification or add additional context in comments.

Comments

2

I have the idea to use Html Agility Pack to parse html.

HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument(); doc.LoadHtml(html); // this is your string var divs = doc.DocumentNode.Elements("div") .Select(div => string.Format("<div>{0}</div>", div.LastChild.InnerText)); 

Comments

0

you can use regex

 string val = @"<div><span class=""help"">This is text.</span>Hello, this is text.</div><div>I have a question.<span class=""help"">Hi</span></div>"; Regex reg = new Regex("<span .+?</span>", RegexOptions.IgnoreCase | RegexOptions.Singleline); string ret = reg.Replace(val, ""); Debug.WriteLine(ret); 

3 Comments

-1: Don't use a regex to parse html
why? I would think it is faster than parse html.
-2

get the elements to contain the runat="server" so they can be accessed from the codebehind and then when it is suitable try getting the element by its id name and do either element.innerHTML = ""; or element.innerText = "";

5 Comments

sorry but how do you -1 the comment? this does what you want to do
I am appreciated with your comment. But as you can see, I was meaning to handle html strings.
well then you should have said so in the description. dont expect everyone to just guess what you want.
sorry bro~ it was all my mistake. please do not get pissed on me.
Drakoumel, you have a tendency to come off very aggressive and defensive. Would suggest actual communication.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.