Reputation: 1223
I have a string from an HTML enabled email of something like so:
</div><span color="red">Hi how are you?!</span></div><table><tr><td>Company Information</td></tr></table>
and so on [its a long string of stuff but you get the idea. There are <div>
..<spans>
..<table>
and so forth.
I want to display the text in the text box formatted like html [which will format it based on the <table>
..<span>
and so forth while removing the actual text of <span>
and so forth from the textbox's text.
I need this to happen because I get a page error because it reads the <span>
and etc as being a security issue.
My current way of reading the whole text and removing the issues are like so:
If Not DsAds.Tables(0).Rows(0).Item(0) Is DBNull.Value Then
Dim bodyInitial As String = DsAds.Tables(0).Rows(0).Item(0).ToString()
Dim newbody As String = bodyInitial.Replace("<br>", vbNewLine)
newbody = newbody.Replace("<b>", "")
newbody = newbody.Replace("</b>", "")
Bodylisting.Text = newbody
End If
I tried to encorporate the following:
Dim bodyInitial As String = DsAds.Tables(0).Rows(0).Item(0).ToString()
Dim myEncodedString As String
' Encode the string.
myEncodedString = bodyInitial.HttpUtility.HtmlEncode(bodyInitial)
Dim myWriter As New StringWriter()
' Decode the encoded string.
HttpUtility.HtmlDecode(bodyInitial, myWriter)
but I get errors with HTTpUtility and strings
Question:
So my question is, is there a way to actually see the HTML formatting and fill the textbox that way, or do I have to stick with my .Replace
method?
<asp:TextBox ID="Bodylisting" runat="server" style="float:left; margin:10px; padding-bottom:500px;" Width="95%" TextMode="MultiLine" ></asp:TextBox>
Upvotes: 1
Views: 874
Reputation: 1414
I suggest you investigate HtmlAgilityPack. This library includes a parser giving you the ability to 'select' the <span>
tags. Once you have them, you can strip them out, or grab the InnerHtml and do further processing. This is an example of how I do something similar with it.
Private Shared Function StripHtml(html As String, xPath As String) As String
Dim htmlDoc As New HtmlDocument()
htmlDoc.LoadHtml(html)
If xPath.Length > 0 Then
Dim invalidNodes As HtmlNodeCollection = htmlDoc.DocumentNode.SelectNodes(xPath)
If Not invalidNodes Is Nothing Then
For Each node As HtmlNode In invalidNodes
node.ParentNode.RemoveChild(node, True)
Next
End If
End If
Return htmlDoc.DocumentNode.WriteContentTo()
End Function
Upvotes: 1