Jun
30

Using CData to store html in xml c#

I always forget this when I am trying to add html tags to an xml file.

using CDATA will allow this more easily preserving the html.

using System;
using System.IO;
using System.Xml;

public class XmlCDataHtmlExample
{
public static void Main()
{
XmlDocument doc = new XmlDocument();
doc.LoadXml("");

//Create a CData section.
XmlCDataSection CData;
CData = doc.CreateCDataSection("Here is a paragraph and I wan't to save the paragraph tags in XML");

//Add the new node to the document.
XmlElement root = doc.DocumentElement;
root.AppendChild(CData);

Console.WriteLine("Display the modified XML...");
doc.Save(Console.Out);

Console.ReadLine();
}
}

-- Lee
 

Microsoft Certification

MCAD MCSD

Photography Galleries