防止XDocument或XElement表单编码内容 [英] Prevent XDocument or XElement form Encoding content

查看:45
本文介绍了防止XDocument或XElement表单编码内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我调用XDocument.Save时,它正在对我的html < br/> 标记进行编码,有没有办法防止这种情况发生?

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"));

输出是:

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

我会喜欢的输出:

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

推荐答案

这是预期的行为:您将 XElement 的内部文本设置为该字符串.需要进行编码,否则会产生多个标签.

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.

由于您实际上希望拥有多个标签,因此需要创建它们.最简单的方法是使用 XElement.Parse :

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);

这篇关于防止XDocument或XElement表单编码内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆