-1

I have below HTML text retrieved from sql server.

"<span style="color: rgb(36, 39, 41); font-family: Arial, &quot;Helvetica Neue&quot;, Helvetica, sans-serif; font-size: 15px; background-color: rgb(255, 255, 255);">So, first method looks nice and it's easy but is there is any simpler way to write the second function? Both are working well and giving a correct output.</span>" 

I want to convert html to plain text and set it on a label in asp.net.

To do this:

  1. I have retrieved data from sql server in Dataset.
  2. Set above retrieved to label

         <h3 id="lblqNotes" runat="server" class="text-questiondata" style="color:#1E90FF">
       </h3>
    

In code behind:

  lblqNotes.InnerText = System.Net.WebUtility.HtmlDecode(values[2]);

But output on label is still html code, not plain text.

Thanks for your help.

Dheeraj Kumar
  • 3,917
  • 8
  • 43
  • 80
  • Do you mean HtmlEncode? https://msdn.microsoft.com/en-us/library/w3te6wfz(v=vs.110).aspx – TigOldBitties Dec 11 '16 at 13:17
  • HtmlEncode is not for converting into PlainText. is it? – Dheeraj Kumar Dec 11 '16 at 13:20
  • Why don't you read what it does, friend? – TigOldBitties Dec 11 '16 at 13:21
  • "HTML encoding makes sure that text is displayed correctly in the browser and not interpreted by the browser as HTML. For example, if a text string contains a less than sign (<) or greater than sign (>), the browser would interpret these characters as the opening or closing bracket of an HTML tag. When the characters are HTML encoded, they are converted to the strings < and >, which causes the browser to display the less than sign and greater than sign correctly." – TigOldBitties Dec 11 '16 at 13:22
  • Understood. But that's not what I am looking for. What I need to do is to convert it into plain text. For ex: HTML hello. output should be hello in bold – Dheeraj Kumar Dec 11 '16 at 13:24
  • you mean Raw? https://msdn.microsoft.com/en-us/library/gg480740(v=vs.118).aspx or decode? https://msdn.microsoft.com/en-us/library/system.web.httputility.htmldecode(v=vs.110).aspx – TigOldBitties Dec 11 '16 at 13:26
  • is HtmlHelper class only for mvc? – Dheeraj Kumar Dec 11 '16 at 13:36
  • It's a library like any other that you can reference. It would be overkill in this case, but you didn't say you're not using mvc. – TigOldBitties Dec 11 '16 at 13:38
  • Yeah. its not MVC. How do I make it work? – Dheeraj Kumar Dec 11 '16 at 13:39
  • How about you change lblqNotes.InnerText to lblqNotes.InnerHtml? – TigOldBitties Dec 11 '16 at 13:41

2 Answers2

2

You can achieve this by using the XmlDocument:

    static void Main(string[] args)
    {
        var xmlDocument = new XmlDocument();
        var html = @"<span style=""color: rgb(36, 39, 41); font - family: Arial, &quot; Helvetica Neue&quot;, Helvetica, sans - serif; font - size: 15px; background - color: rgb(255, 255, 255); "">So, first method looks nice and it's easy but is there is any simpler way to write the second function? Both are working well and giving a correct output.</span>";
        xmlDocument.LoadXml(html);

        var text = xmlDocument.InnerText;
        // So, first method looks nice and it's easy but is there is any simpler way to write the second function? Both are working well and giving a correct output.
    }
Kevin Smith
  • 13,746
  • 4
  • 52
  • 77
  • Amazing. Simplest thing to do. Just a question. How do I use "@" with a variable which has html code in LoadXmL method like xmlDocument.LoadXml(@)?? – Dheeraj Kumar Dec 11 '16 at 13:48
  • @ is just a verbatim string, I've updated the question to pull out that variable – Kevin Smith Dec 11 '16 at 13:51
  • that works, but in my case I am pulling data from db and storing data into a variable. So how do I put @ to that varibale? I tried this string var= @values[2]; but it throws error. here values[2] have html code. – Dheeraj Kumar Dec 11 '16 at 13:57
  • Just removed the @ and will work fine for your problem – Kevin Smith Dec 11 '16 at 14:01
  • But in case where I have multiple tags in a variable, like this: I have a given String that represents the age in format:
    , It throws error saying there are multiple root elements. Any suggestion for this?
    – Dheeraj Kumar Dec 11 '16 at 14:06
  • Just wrap the whole bock in a tag – Kevin Smith Dec 11 '16 at 14:26
0

Try this code to strip out the Html by using a regex : output = Regex.Replace(source, "<[^>]*>", string.Empty);

Voice Of The Rain
  • 495
  • 1
  • 8
  • 24
  • This makes me think of this beauty http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags – TigOldBitties Dec 11 '16 at 13:21
  • It is just for stripping html characters. What I need to do is to convert it into plain text. For ex: HTML hello. output should be hello in bold. – Dheeraj Kumar Dec 11 '16 at 13:23
  • What you need is an Html parser check this post http://stackoverflow.com/questions/56107/what-is-the-best-way-to-parse-html-in-c – Voice Of The Rain Dec 11 '16 at 13:26