Strange characters at top of transformation output  
Author Message
jsm11482





PostPosted: XML and the .NET Framework, Strange characters at top of transformation output Top

Hi, I am getting some strange characters at the top of all of my transforms using the XslCompiledTransform. Please download the zip containing the XML file, the XSLT file and the resulting output of the transformation.  Below is a code sample, I have ommitted a number of unimportant lines of code. If you view the Address.java file in a hex-editor you will see what I am talking about.  Thank you!

Download the ZIP!

XslCompiledTransform transform = new XslCompiledTransform();
XmlReader reader = null;

transform.Load(_project.ExtendedClassTemplatePath);
reader = XmlReader.Create(basePath + tm.ClassName + ".xml");
transform.Transform(reader, null, javaFile);
javaFile.Close();
reader.Close();



.NET Development34  
 
 
jsm11482





PostPosted: XML and the .NET Framework, Strange characters at top of transformation output Top

Lame. So if I do the following, just before the transformation it works fine...explanations

javaFile.Write(Encoding.Default.GetBytes("\n"), 0, 1);


 
 
Sinan Ussakli - MSFT





PostPosted: XML and the .NET Framework, Strange characters at top of transformation output Top

It's most likely because your encoding is UTF-8. Change it to Ascii and you won't see these chars. The first 2 bytes are UTF8 markers. And non utf 8 viewers may have trouble viewing it.



 
 
Oleg Tkachenko





PostPosted: XML and the .NET Framework, Strange characters at top of transformation output Top

Most likely that's unicode byte order mark. It's optional in UTF-8, but XmlWriter by default creates it. While perfectly ok in XML, it might be bad surprise for some Java tools. You need to suppress BOM generation.
When creating javaFile use "new UTF8Encoding(false)" as file encoding - that will create UTF-8 encoded file with no BOM character.