XmlTextWriter.WriteCData writes out a block containing the specified text.
using System;
using System.IO;
using System.Xml;
public class Sample
{
private const string filename = "sampledata.xml";
public static void Main()
{
XmlTextWriter writer = null;
writer = new XmlTextWriter (filename, null);
writer.Formatting = Formatting.Indented;
writer.WriteStartDocument();
String PItext="type='text/xsl' href='book.xsl'";
writer.WriteProcessingInstruction("xml-stylesheet", PItext);
writer.WriteDocType("book", null , null, "<!ENTITY h 'hardcover'>");
writer.WriteComment("sample XML");
writer.WriteStartElement("book");
writer.WriteAttributeString("genre", "Computer");
writer.WriteAttributeString("ISBN", "1-8630-014");
writer.WriteElementString("title", "C#");
writer.WriteStartElement("style");
writer.WriteEntityRef("h");
writer.WriteEndElement();
writer.WriteElementString("price", "19.95");
writer.WriteCData("Prices 15% off!!");
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Flush();
writer.Close();
XmlDocument doc = new XmlDocument();
doc.PreserveWhitespace = true;
doc.Load(filename);
Console.Write(doc.InnerXml);
}
}
Related examples in the same category