home / software / tips and tricks / Special Character Conversion when Writing XML Content using c#

Special Character Conversion when Writing XML Content using c#

Updated:  05/06/2010 13:05 PM
Author:  Shiju Mathews

Status:    Resolved.


The XmlWriter includes a method, WriteRaw(), which allows you to write out raw markup manually. This method prevents special characters from being escaped. This is in contrast to the WriteString method, which escapes some strings to their equivalent entity reference. The characters that are escaped are given in the XML 1.0 recommendation in section 2.4 Character Data and Markup, and section 3.3.3 Attribute-Value Normalization of the Extensible Markup Language (XML) 1.0 (Second Edition) Recommendation. If the WriteString method is called when writing an attribute value, it escapes ' and ". Character values 0x-0x1F are encoded as numeric character entities � through , except for the white space characters 0x9, 0x10, and 0x13.

So the guiding principle of when to use WriteString() or WritingRaw() is that WriteString is used when you need to walk through every character looking for entity characters, and WriteRaw writes exactly what it is given.

The following example shows the difference between the WriteString and WriteRaw method when given the "<" character. This code sample uses WriteString.

Tags: Special Character Conversion when Writing XML Content using c#
Updated on: March 2024