Remoing <p> tag from string in c# RRS feed

  • Question

  • User-1569093213 posted

    Hello All,

    I have string with multiple html tags like div,p.

    I want to remove the p tag from string how can I do that from c# code?

    Please suggest solution


    Monday, June 1, 2020 10:14 AM

All replies

  • User475983607 posted

    Use a tool designed to parse HTML like the HTML Agility Pack.


    Monday, June 1, 2020 10:16 AM
  • User-1569093213 posted

    Can we have any other option than using the tool?

    Monday, June 1, 2020 10:29 AM
  • User1034446946 posted

    var htmlString = "<p>some text</p>";

    var withoutHtmlstring = htmlString.replace("<p>", "").replace("</p>", "");

    Monday, June 1, 2020 11:04 AM
  • User475983607 posted


    Can we have any other option than using the tool?

    It is very difficult to provide assistance when there are unknown restrictions on solutions.  The HTML Agility Pack is a C# API designed to parse HTML.  Why is the HTML Agility Pack not an option?  It seems to meet your original requirement of using C#.

    .NET has robust XML APIs that have been around for years that you can use to parse well forms HTML.  There's tons of examples in the official docs.

    There's also RegEx.

    Is the HTML coming from user input?  Can you explain the general design intention and problem you are trying to solve?

    Monday, June 1, 2020 11:30 AM
  • User-2054057000 posted

    You can simply use Regular Expressions to remove all html tags. Like this code:

    String result = Regex.Replace(htmlDocument, @"<[^>]*>", String.Empty);

    Tuesday, June 2, 2020 3:10 PM