I am experimenting with using VB.Net's Xml Literals as a templating system for generating HTML. What I am wondering if there is any way to control the escaping. For example if I have code similar to the following
Dim param1Value as String = "test"
Dim html = <div>
<a id="myLink" href="<%= "/mysite/myfile?param1=" + param1Value + "¶m2=test2" %>"
</div>
This will produce a link with the ampersands xml encoded (&
). Currently, I am just un-escaping these when transforming the xmlElement to a string. I am assuming that is my only option as anything else produces invalid XML, but I was wondering if there was any way to specify that an XML Literal is a raw value and does not need Xml Escaped.
Years ago, I created an ASP.NET MVC View Engine using VB.NET XML Literals in which I had to solve this exact problem. There was no way to tell the Xml literals part of vb.net
to ignore &
characters. My particular scenario was trying to include HTML entities (like ©
) within the Xml literals.
What I ended up with was using <amp />
in all the places where I needed the &
character within an XML Literal and then just doing a .Replace("<amp />", "&")
when rendering the HTML. See the Render
method in VbView.vb.