How to prevent HTML encoding for embedded expressions in XML Literals (VB.NET)?
Asked Answered
N

2

7

With the following code:

Dim x As System.Xml.Linq.XElement = _
<div>
<%= message.ToString() %>
</div>
Dim m = x.ToString()

...if message is HTML, then the < and > characters get converted to &lt; and &rt;.

How can I force it to skip this encoding?

Ness answered 12/11, 2008 at 21:56 Comment(1)
Wow, it's been a whole year, but it's not too late to accept an answer for this question!Craig
C
7

What is the type of your message variable? If message is an XElement, then just leave off the .ToString call like this:

Dim x As System.Xml.Linq.XElement = _
    <div>
        <%= message %>
    </div>
Dim m = x.ToString()

If message is some other type (like StringBuilder), then do this:

Dim x As System.Xml.Linq.XElement = _
    <div>
        <%= XElement.Parse(message.ToString()) %>
    </div>
Dim m = x.ToString()
Craig answered 30/4, 2009 at 6:14 Comment(0)
G
1

You need to open the HTML snippit as an XML document and append the document node to the Div node you are creating.

If you want to add XML (or HTML) to an existing XML document then you have to add it as XML and not as text (cause that gets encoded).

Grannias answered 13/11, 2008 at 1:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.