tags:

views:

1331

answers:

4

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>
+1  A: 

Insert it into a CDATA section:

<![CDATA[some <b>bolded</b> text]]>
Anton Gogolev
+4  A: 

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 don't think that is what you mean either.

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


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);
Marc Gravell
The text comes from a setting and It must be inserted as Text or as XML. The final XML should not contain CDATA sections.
Drejc
Then I expect you want InnerXml above...
Marc Gravell
The outer and inner Xml do the trick, with additional logic I think it will work.
Drejc
A: 

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.

Paolo Tedesco
A: 

@Mark Gravell:

The LoadXml statement using

Xml tags and a foo tag in between throws Exception: Illegal characters in path.

GanYo