Prevent XDocument or XElement form Encoding content

坚强是说给别人听的谎言 提交于 2020-05-12 09:01:12

问题


When I call XDocument.Save it is encoding my html <br/> tag, is there a way to prevent this?

XDocument xDoc = new XDocument(new XElement("desc","jon skeet <br/> knows, the <br/> answer"));
xDoc.Save(Server.MapPath("~/tempUploads/encodeTest.xml"));

OUTPUT IS:

<?xml version="1.0" encoding="utf-8"?>
<desc>jon skeet &lt;br/&gt; knows, the &lt;br/&gt; answer</desc>

OUTPUT I WOULD LIKE:

<?xml version="1.0" encoding="utf-8"?>
<desc>jon skeet <br/> knows, the <br/> answer</desc>

回答1:


That's expected behavior: You set the inner text of the XElement to that string. It needs to be encoded, otherwise it would create multiple tags.

As you actually want to have multiple tags, you need to create them. The easiest way would be to use XElement.Parse:

var content = XElement.Parse("<desc>jon skeet <br/> knows, the <br/> answer</desc>");
var xDoc = new XDocument(content);


来源:https://stackoverflow.com/questions/12838358/prevent-xdocument-or-xelement-form-encoding-content

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!