C# 将文本节点插入到包含 XML 的 XML 文档中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/949122/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 04:02:03  来源:igfitidea点击:

Insert text node to XML document containing XML

c#xml

提问by Drejc

I'm building a XML document dynamically where I create a a text node with CreateTextNode(text) method.

我正在动态构建一个 XML 文档,在其中使用 CreateTextNode( text) 方法创建一个文本节点。

Now in case the textcontains XML it will be XML encoded.

现在,如果文本包含 XML,它将被 XML 编码。

For instance:

例如:

text = "some <b>bolded</b> text"

How do you insert the text without being XML encoded.

如何在不进行 XML 编码的情况下插入文本。

EDIT:

编辑:

I'm building a XML document with XmlDocument and insert elements and nodes. The final output should not contain CDATA sections or encoded parts.

我正在使用 XmlDocument 构建一个 XML 文档并插入元素和节点。最终输出不应包含 CDATA 部分或编码部分。

For instace I want the final output to look like this, where the text comes from a setting:

例如,我希望最终输出看起来像这样,其中文本来自设置:

<root><p>Some <b>bolded</b> text</p></root>

采纳答案by Marc Gravell

If you want the textto be "some <b>bolded</b> text", then encoding is the rightthing - otherwise it isn't (just) a text node. You could CDATA it, but I don't think that is what you mean either.

如果您希望文本"some <b>bolded</b> text",则编码是正确的 - 否则它不是(只是)文本节点。你可以 CDATA 它,但我认为这也不是你的意思。

Do you want the xml contents to be the above? i.e. so that it gets a <b>...</b>elelmentinside it?

您希望 xml 内容是以上内容吗?即它里面有一个<b>...</b>元素



edit: here's code that adds the content via various methods:

编辑:这是通过各种方法添加内容的代码:

        string txt = "some <b>bolded</b> text";
        XmlDocument doc = new XmlDocument();
        doc.LoadXml("<xml><foo/></xml>");
        XmlElement foo = (XmlElement)doc.SelectSingleNode("//foo");

        // text: <foo>some &lt;b&gt;bolded&lt;/b&gt; text</foo>
        foo.RemoveAll();
        foo.InnerText = txt;
        Console.WriteLine(foo.OuterXml);

        // xml: <foo>some <b>bolded</b> text</foo>
        foo.RemoveAll();
        foo.InnerXml = txt;
        Console.WriteLine(foo.OuterXml);

        // CDATA: <foo><![CDATA[some <b>bolded</b> text]]></foo>
        foo.RemoveAll();
        foo.AppendChild(doc.CreateCDataSection(txt));
        Console.WriteLine(foo.OuterXml);

回答by Anton Gogolev

Insert it into a CDATAsection:

将其插入CDATA部分:

<![CDATA[some <b>bolded</b> text]]>

回答by Paolo Tedesco

Use a CDATA node, like this:

使用 CDATA 节点,如下所示:

class Program {

    static void Main(string[] args) {
        XmlDocument d = new XmlDocument();

        XmlNode root = d.CreateNode(XmlNodeType.Element, "root", null);
        d.AppendChild(root);

        XmlNode cdata = d.CreateNode(XmlNodeType.CDATA, "cdata", null);
        cdata.InnerText = "some <b>bolded</b> text";
        root.AppendChild(cdata);

        PrintDocument(d);
    }

    private static void PrintDocument(XmlDocument d) {
        StringWriter sw = new StringWriter();
        XmlTextWriter textWriter = new XmlTextWriter(sw);
        d.WriteTo(textWriter);
        Console.WriteLine(sw.GetStringBuilder().ToString());
    }
}

This will print

这将打印

<root><![CDATA[some <b>bolded</b> text]]></root>

The CDATA section looks ugly, but that's how you insert text without having to escape characters...

CDATA 部分看起来很难看,但这就是您无需转义字符即可插入文本的方式...

Otherwise, you can use the InnerXml property of a node:

否则,您可以使用节点的 InnerXml 属性:

    static void Main(string[] args) {
        XmlDocument d = new XmlDocument();

        XmlNode root = d.CreateNode(XmlNodeType.Element, "root", null);
        d.AppendChild(root);

        XmlNode cdata = d.CreateNode(XmlNodeType.Element, "cdata", null);
        cdata.InnerXml = "some <b>bolded</b> text";
        root.AppendChild(cdata);

        PrintDocument(d);
    }

This prints

这打印

<root><cdata>some <b>bolded</b> text</cdata></root>

But pay attention when you deserialize it, as the content of the "cdata" node is now actually three nodes.

但是在反序列化时要注意,因为“cdata”节点的内容现在实际上是三个节点。