Drejc
Drejc

Reputation: 14286

Insert text node to XML document containing XML

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

Now in case the text contains XML it will be XML encoded.

For instance:

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

How do you insert the text without being XML encoded.

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.

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>

Upvotes: 3

Views: 18801

Answers (3)

Marc Gravell
Marc Gravell

Reputation: 1062510

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

Do you want the XML contents to be the above text (so that it gets a <b>...</b> element inside it)?


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

Upvotes: 7

Paolo Tedesco
Paolo Tedesco

Reputation: 57172

Use a CDATA node, like this:

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...

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

    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.

Upvotes: 0

Anton Gogolev
Anton Gogolev

Reputation: 115691

Insert it into a CDATA section:

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

Upvotes: 2

Related Questions