Consider the following code in VB9:
Dim text = "Line1<br/>Line2"
Dim markup = <span><%= text %></span>.ToString
While I was hoping markup would end up being <span>Line1<br/>Line2</span>
, it actually evaluates to <span>Line1<br/>Line2</span>
.
Is there any way to get the value of the variable not to be HTML encoded?
P.S.: This is an oversimplified example of what I'm trying to do. I know this could be rewritten a number of ways to make it work, but the way my code is constructed, un-encoding the variable would be optimal. If that's not possible, then I'll go down a different road.
More detail: The "text" is coming from a database, where a user can enter free-form text, with carriage returns. The output is HTML, so somehow I need to get the value from the database and convert the carriage returns to line breaks.
This behavior is "By Design." When embedding a string expression inside an XML literal the value will be escaped to be a legal string value.
To get the behavior you are looking for you'll need to be embedding an XElement/XNode within an XML literal. Take the following example. It will correctly keep the <br/>
tag as an XElement.
Dim text2 = <a>Line<br/>Line</a>
Dim markup2 = <span><%= text2 %></span>.ToString
One way to achieve this is to fake an XElement. To make the text a valid string, simply wrap it on both ends with a normal tag, <a>
for example. This is now a parsable XML fragment. Once you have an XElement, it's easy to get the embedded behavior you are looking for
Dim text = "Line1<br/>Line2"
Dim text2 = XElement.Parse("<a>" + text + "</a>")
Dim markup = <span><%= text2.Nodes %></span>.ToString
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With