Today, I had to create a csv text file in plain ASCII text. The source data was in xml format.
The first solution that comes in mind is the use of an xslt transformation to tranform the xml file into the ASCII text file. However, the xml file can (and will) contain unicode characters. According to the spec, these characters must be replaced by a '^' character in the csv file.
This can be accomplished by specifying an Encoding type with an EncoderReplacementFallback instance. See the code below:
//Create an XsltArgumentList.
XsltArgumentList args = new XsltArgumentList();
...
// Create the transformation class
XslCompiledTransform xslt = new XslCompiledTransform(true);
// Load the xslt file
xslt.Load(config.TransformationFile);
// Create an encoding type so that unicode characters are translated into a '^'
Encoding encoding = Encoding.GetEncoding("us-ascii",
new EncoderReplacementFallback("^"),
new DecoderReplacementFallback("ERROR"));
// Create an XmlTextWriter to output to file.
using (TextWriter writer = new StreamWriter(csvOutputFilename, false, encoding))
xslt.Transform(reader, args, writer);
What happens here is that a 'us-ascii' Encoding type is created with an EncoderReplacementFallback instance. This ensures that all characters that are not part of the 'us-ascii' encoding are replaced by the '^' character.
This encoding is later used for creating the TextWriter with the specified encoding.
For more information see the microsoft documentation:
http://msdn2.microsoft.com/en-us/library/system.text.encoderreplacementfallback.aspx