I'm using XmlTextWriter
class in my project. i don't know, how to use CDATA in Xml. Can anyone help me?
objX.WriteElementString("category", c.DeepestCategoryName);
As noted by others, use WriteCData
if you want to write a CDATA section explicitly. Here is a general-purpose extension method that I use to write a CDATA element 'automatically' if the text contains certain characters:
public static void WriteElementContent(this XmlWriter writer, string content)
{
if (String.IsNullOrEmpty(content))
{
return;
}
// WriteString will happily escape any XML markup characters. However,
// for legibility we write content that contains certain special
// characters as CDATA
const string SpecialChars = @"<>&";
if (content.IndexOfAny(SpecialChars.ToCharArray()) != -1)
{
writer.WriteCData(content);
}
else
{
writer.WriteString(content);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With