1

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>

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

8 Comments

Thank you for your suggestions and references. I will play with the following coding. Once again, I appreciate it!
Also, if you don't mind me asking, what Import do you use to define New HtmlDocument() ?
Glad I could help Imports HtmlAgilityPack will let you get to HtmlDocument
When I put Imports HtmlAgilityPack HtmlAgilityPack has a green line under it with no suggestions as if it doesn't exist
Did you download the file and add a reference to the DLL to your project?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.