views:

22

answers:

2

I have a string in the database which contains angle brackets (less-than/greater-than). I need to insert the string into an XElement and then serialise it to a client device. Now the html entities obviously get encoded so the brackets appear as < and > and the best way may be is to use the HttpUtility.HtmlDecode to decode the entities back on the client device. But I am wondering is there anyway to escape the encoding and have these entities send as is?

A: 

Use SecurityElement.Escape Method

Orsol
That still turns it into < and > (doesn't not keep it as < and >)
Shahid
You need to handled this on client. With < and > xml will be malformed.
Orsol
A: 

If you set the XElement.Value property, yes, it will be encoded when it's serialized. But when you deserialize it on the client and access the XElement.Value property, it will be unencoded. See this quick sample:

var htmlData = "<some data>";
XElement element = new XElement("data");
element.Value = htmlData;

var xml = element.ToString();
var data = element.Value;
if (data != htmlData)
    throw new NotImplementedException("this didn't work");

If you set a breakpoint, you'll see the variable 'xml' does contain the encoded html string.

Patrick Steele