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.