开发者

Why is xmlWriter writing ASCII characters in the output xml file?

开发者 https://www.devze.com 2023-01-25 01:56 出处:网络
Here\'s how I set up my xmlWriter and xmlWriterSettings: XmlWriterSettings settings = new XmlWriterSettings();开发者_StackOverflow社区

Here's how I set up my xmlWriter and xmlWriterSettings:

        XmlWriterSettings settings = new XmlWriterSettings();开发者_StackOverflow社区
        settings.Encoding.Equals("UTF-8"); 
        settings.Indent = true;
        settings.OmitXmlDeclaration = true;
        settings.NewLineOnAttributes = true;           
        settings.CheckCharacters = true;
        settings.OmitXmlDeclaration = false;

        XmlWriter xmlWriter =  XmlWriter.Create(otptFile,settings);

I produce a file that looks like a valid xml file, but when I run it through a validator I get an error: ascii "\EF" does not map to unicode.

If I open this file in textpad and just save it, this error goes away and the file is valid.

I am using streamReader to read in an ascii file. I checked if I needed to set something in the streamreader declaration, but from what I understand UTF-8 is the default.

I am trying to understand why my program has ascii values if I have set the encoding to UTF-8 and how I can get rid of the easily in my code. Thanks for your help!


The byte sequence 0xEF,0xBB,0xBF is the UTF-8 byte order mark (BOM).

If you don't want to output the BOM, set the Encoding property to an UTF8Encoding instance with the encoderShouldEmitUTF8Identifier constructor argument set to false:

settings.Encoding = new UTF8Encoding(false);
0

精彩评论

暂无评论...
验证码 换一张
取 消