Skip to content Skip to sidebar Skip to footer

How Can I Remove With Specific Tags From Html

Possible Duplicate: How to use HTML Agility pack I have html code below:
This is text.Hello, this is text.

Solution 1:

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;

Solution 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));

Solution 3:

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);

Solution 4:

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 = "";


Post a Comment for "How Can I Remove With Specific Tags From Html"