Stop CDATA tags from being output-escaped when writing to XML in C#

Posted by Smallgods on Stack Overflow See other posts from Stack Overflow or by Smallgods
Published on 2010-06-11T10:24:48Z Indexed on 2010/06/11 10:32 UTC
Read the original article Hit count: 255

Filed under:
|
|

We're creating a system outputting some data to an XML schema. Some of the fields in this schema need their formatting preserved, as it will be parsed by the end system into potentially a Word doc layout. To do this we're using <![CDATA[Some formatted text]]> tags inside of the App.Config file, then putting that into an appropriate property field in a xsd.exe generated class from our schema. Ideally the formatting wouldn't be out problem, but unfortunately thats just how the system is going.

The App.Config section looks as follows:

<header>
<![CDATA[Some sample formatted data]]>
</header>

The data assignment looks as follows:

HeaderSection header = ConfigurationManager.GetSection("header") as HeaderSection;
report.header = "<[CDATA[" + header.Header + "]]>";

Finally, the Xml output is handled as follows:

xs = new XmlSerializer(typeof(report));
fs = new FileStream (reportLocation, FileMode.Create);
xs.Serialize(fs, report);
fs.Flush();
fs.Close();

This should in theory produce in the final Xml a section that has information with CDATA tags around it. However, the angled brackets are being converted into &lt; and &gt;

I've looked at ways of disabling Outout Escaping, but so far can only find references to XSLT sheets. I've also tried @"<[CDATA[" with the strings, but again no luck.

Any help would be appreciated!

© Stack Overflow or respective owner

Related posts about c#

Related posts about Xml