开发者

C Sharp serialise XML with HTML entities

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 hav开发者_JAVA百科e these entities send as is?


Use SecurityElement.Escape Method


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜