views:

14

answers:

1

Hi, my code is outputting some weird character at the very start of my XSLT output XML and neither Visual Studio 2008 or notepad show it up. But it's definitely there because VS lets me delete it and will then auto-format the XML properly. How do I stop this? Here's my code:

    // create the readers for the xml and xsl
    XmlReader reader = XmlReader.Create(
        new StringReader(LoadFileAsString(MapPath(xslPath)))
    );
    XmlReader input = XmlReader.Create(
        new StringReader(LoadFileAsString(MapPath(xmlPath)))
    );

    // create the xsl transformer
    XslCompiledTransform t = new XslCompiledTransform(true);
    t.Load(reader);

    // create the writer which will output the transformed xml
    StringBuilder sb = new StringBuilder();
    //XmlWriterSettings tt = new XmlWriterSettings();
    //tt.Encoding = Encoding.Unicode;
    XmlWriter results = XmlWriter.Create(new StringWriter(sb));//, tt);

    // write the transformed xml out to a stringbuilder
    t.Transform(input, null, results);

    // return the transformed xml
    WriteStringAsFile(MapPath(outputXmlPath), sb.ToString());


    public static string LoadFileAsString(string fullpathtofile)
    {
        string a = null;
        using (var sr = new StreamReader(fullpathtofile))
            a = sr.ReadToEnd();
        return a;
    }

    public static void WriteStringAsFile(string fullpathtofile, string content)
    {
        File.WriteAllText(fullpathtofile, content.Trim(), Encoding.Unicode);
    }
+1  A: 

That thing at the beginning of your XML output document is most likely a byte-order-mark or BOM, which indicates whether the bytes in your Unicode output are in big-endian or little-endian order.

This BOM might be useful for consumers of your XML document; however, in some cases it might lead to problems and then it is better not to create it.

You can specify whether a BOM is created using the Encoding specified via XmlWriterSettings:

XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();
xmlWriterSettings.Encoding = new UTF8Encoding(false);

The code above will create your document using UTF8 encoding. This is most likely what you want to have unless your consuming system explicitly asks for UTF16/Unicode encoding or you are dealing with Asian character.

To create a UTF16/Unicode encoded document use UnicodeEncoding with the second parameter set to false:

XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();
xmlWriterSettings.Encoding = new UnicodeEncoding(false, false);
0xA3
Thank you - that was exactly it. However, I was stamping on my own toes by resetting the encoding in the File.WriteAllText call - so I replaced Encoding.Unicode with new UnicodeEncoding(false, false) and it works. With that in place I was able to comment out the XmlWriterSettings entirely.
Matt W