Groups | Blog | Home
all groups > dotnet xml > march 2004 >

dotnet xml : ASCII encoding a file


steven.skarupa NO[at]SPAM usa.net
3/25/2004 7:38:02 AM
I am having problems creating a file from the result of a
XslTransform.Transform operation. This is XML->EDI conversion and the
resulting text has to be in ASCII format, but no matter what I do the
output of the file is in UTF-8.

Please note: I'm using .NET Framework 1.0.

Here is the code that I have been attempting to use:

public void Test()
{
string templateFullPath = @"c:\_bin\temp\test.xsl";
string outputFile = @"c:\_bin\temp\output4.txt";
string inputFile = @"c:\_bin\temp\test.xml";

Encoding encoding = System.Text.Encoding.ASCII;
StreamWriter sw = new StreamWriter(outputFile,false,encoding);

XmlDocument doc = new XmlDocument();
doc.Load(inputFile);

XslTransform template = new XslTransform();
template.Load(templateFullPath);
template.Transform(doc,null,sw);

Console.WriteLine(sw.Encoding.ToString());
sw.Flush();
sw.Close();

string encodingName = DetermineFileType(outputFile);
Console.WriteLine("text file encoding = " + encodingName);
}
public string DetermineFileType(string aFileName)
{
string sEncoding = string.Empty;

StreamReader oSR = new StreamReader(aFileName, true);
oSR.ReadToEnd();
sEncoding = oSR.CurrentEncoding.EncodingName;

return sEncoding;
}
The output of the transformation is supposed to be a single line with
header information and multiple lines with "item" data separated by
hard returns.

The really strange thing is that the first WriteLine (querying the
encoding of the streamwriter shows ASCII), but the second one shows
UTF-8. I would doubt the DetermineFileType results, but when I try to
open the file in notepad (or other text editors) the output is one
long line of text with a little box where the hard return should be.

What am I doing wrong?

Thanks in advance.

steven.skarupa NO[at]SPAM usa.net
3/25/2004 11:16:18 AM
Follow up: Since I posted this I have found that in the stylesheet
the hard return was encoded in unicode instead of ascii (

instead of 
). This seemed to make it so that the file would
be open as ascii in Notepad and UltraEdit, but the my
"DetermineFileType" function still shows UTF-8... My immediate
problem has been solved but the question still remains: Is the
resulting file encoded in ASCII?

Michael Schöller
4/2/2004 11:12:51 AM
Try using this code. The code is written in Framework 1.1 but it was
originaly written in 1.0 and as I remember there are only minor changes
between that two versions (I think only an parameter was added in some of
the functions)
public virtual void writeCsv(DataSet aussendungsDaten, string xsltFileName)

{

FileStream sw = null;

XmlDocument xmlDoc = new XmlDocument();

MemoryStream dataXmlStream = new MemoryStream();

XslTransform xslt = new XslTransform();

XsltArgumentList argList = new XsltArgumentList();;

XmlTextReader xmlTxtReader = null;

try

{

dataXmlStream = new MemoryStream();

aussendungsDaten.WriteXml(dataXmlStream, XmlWriteMode.WriteSchema);


dataXmlStream.Seek(0, SeekOrigin.Begin);



xmlTxtReader = new XmlTextReader(dataXmlStream);

xmlDoc.Load(xmlTxtReader);

xslt.Load(xsltFileName);


sw = new FileStream(fullFileName + ".csv", System.IO.FileMode.Create);

xslt.Transform(xmlDoc, argList, sw, null);

}

catch(Exception ex)

{

throw new Exception("Transformation of " + xsltFileName + " failed", ex);

}

finally

{

if(xmlDoc != null)

{

xmlDoc.RemoveAll();

}

if(dataXmlStream != null)

{

dataXmlStream.Close();

dataXmlStream = null;

}

if(xmlTxtReader != null)

{

xmlTxtReader.Close();

xmlTxtReader.Close();

}

if(sw != null)

{

sw.Close();

sw = null;

}

}



int the xslt file add teh following line behind the beginning of the

xsl:stylesheet section

<xsl:output method="text" version="1.0" encoding="iso8859-1" indent="yes"/>

well iso8859-1 is ansi but the 127 chars are equal. and ASCII has only 127
chars so the file sould be an valid ascii file.


[quoted text, click to view]

AddThis Social Bookmark Button