我正在使用 Xml 和 Linq 对 XHTML 文档执行 XML 操作。
将其转换回字符串以输出到浏览器时,它会在将 XElement 创建为
<script />
.
这会阻止它被市场上的大多数浏览器正确处理。
我想以一种输出脚本标记的方式执行 XElement 到 String 的转换,如下所示<script></script>
有人可以帮我吗?提前致谢。:)
首先编辑
提供更多信息,数据来自 MSSQL 2008R2 数据库中的 xml 字段。它从 xml 字段加载为
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js" />
第二次编辑 工作样本
using System;
using System.Linq;
using System.Xml.Linq;
namespace Test1
{
class Test1
{
static void Main(string[] args)
{
XElement element = XElement.Parse("<div><script /><script>Test</script></div>");
var EmptySet = (from e in element.DescendantsAndSelf()
where e.IsEmpty
select e);
foreach(XElement e in EmptySet)
{
e.Value = String.Empty;
}
Console.WriteLine(element.ToString(SaveOptions.None));
}
}
}
及其结果
<div>
<script></script>
<script>Test</script>
</div>