public abstract WriteCData ( string text ) : void | ||
text | string | |
return | void |
using System.Xml; // Create a new XmlWriter on a FileStream // that will write in UTF-8 encoding using (XmlWriter writer = XmlWriter.Create("output.xml", new XmlWriterSettings() { Indent = true })) { writer.WriteStartElement("root"); writer.WriteCData("This is CDATA section"); writer.WriteEndElement(); writer.Flush(); }
using System.Xml; string text = "Some text with special characters < > & \' \""; string cdataText = $""; // Create a new XmlWriter on a FileStream // that will write in UTF-8 encoding using (XmlWriter writer = XmlWriter.Create("output.xml", new XmlWriterSettings() { Indent = true })) { writer.WriteStartElement("root"); writer.WriteCData(cdataText); writer.WriteEndElement(); writer.Flush(); }This example creates an XML document that contains a CDATA section with the text "Some text with special characters < > & \' \"". The text is properly formatted as a CDATA section to ensure that special characters, such as angle brackets and ampersands, are not interpreted as part of the XML markup. Overall, the XmlWriter class provides a flexible and robust way of writing XML data, including character data as CDATA sections.