c# - 在 C# 中编写未转义的 XMLDocument

标签 c# xml xhtml

目前我正在 XmlDocument 中编写 XHTML。这很完美,但我遇到了一个问题。某些 XmlText 元素可以包含诸如   之类的内容。当我想将此类内容写入流时,它使用 innerXML 而不是此类节点的 innerText 值。问题是输出是错误的,因为现在它正在输出  而不是  。在写入流时如何在不执行此类转义的情况下使用 xmlwriter 和 xmldocument?我只想要未转义的输出。

最佳答案

如果您使用 XmlWriter.WriteRaw ,它不会执行任何转义 - 它假定您有原始 XML。

例如:

using System;
using System.Xml;

class Test
{
    static void Main()
    {
        using (XmlWriter writer = XmlWriter.Create(Console.Out))
        {
            writer.WriteStartDocument();
            writer.WriteStartElement("root");
            writer.WriteRaw("<element>&nbsp;</element>");
            writer.WriteEndElement();
            writer.WriteEndDocument();
        }
    }
}

输出:

<?xml version="1.0" encoding="IBM437"?><root><element>&nbsp;</element></root>

关于c# - 在 C# 中编写未转义的 XMLDocument,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1402387/

相关文章:

c# - 给定一个表名列表作为字符串,如何一次查询多个表?

c# - Web 服务调用时长

sql - 在表中保存嵌套 XML 时出现 PL/SQL 错误

xml - 统计数据和元数据交换 (SDMX)

javascript - d3 - 在 svg 元素之上渲染 html 表格

c# - 在 C# 代码中获取 UWP 主题颜色

c# - asp.net core 中 'Configure' 方法有多少重载是可能的?

java - 属性 "Any"已定义。使用 <jaxb :property> to resolve this conflict

css - 输入框对齐?

asp.net - 如何获得干净的 ASP.Net 错误页面?