vb.netxml-literals

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


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?


Solution

  • 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()