How to prevent HTML coding 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 the message is HTML, then <and> characters are converted to &lt; and &rt; .

How can I make it skip this encoding?

+7
source share
2 answers

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

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

If message is a different type (e.g. StringBuilder ), do the following:

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

You need to open the HTML snippet as an XML document and add the node document to the created Div node.

If you want to add XML (or HTML) to an existing XML document, you must add it as XML, not as text (because it is encoded).

+1
source share

All Articles