Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can i remove an outer <p>...</p> from a string

I want to query a string (html) from a database and display it on a webpage. The problem is that the data has a

 <p> around the text (ending with </p>

I want to strip this outer tag in my viewmodel or controlleraction that returns this data. what is the best way of doing this in C#?

like image 726
leora Avatar asked Dec 17 '22 17:12

leora


1 Answers

Might be overkill for your needs, but if you want to parse the HTML you can use the HtmlAgilityPack - certainly a cleaner solution in general than most suggested here, although it might not be as performant:

HtmlDocument doc = new HtmlDocument();
doc.LoadHtml("<p> around the text (ending with </p>");
string result = doc.DocumentNode.FirstChild.InnerHtml;
like image 59
BrokenGlass Avatar answered Jan 01 '23 22:01

BrokenGlass