c# - XDocument: saving XML to file without BOM -
i'm generating utf-8 xml file using xdocument
.
xdocument xml_document = new xdocument( new xdeclaration("1.0", "utf-8", null), new xelement(root_name, new xattribute("note", note) ) ); ... xml_document.save(@file_path);
the file generated correctly , validated xsd file success.
when try upload xml file online service, service says file wrong @ line 1
; have discovered problem caused bom on first bytes of file.
do know why bom appended file , how can save file without it?
as stated in byte order mark wikipedia article:
while unicode standard allows bom in utf-8 it not require or recommend it. byte order has no meaning in utf-8 bom serves identify text stream or file utf-8 or converted format has bom
is xdocument
problem or should contact guys of online service provider ask parser upgrade?
use xmltextwriter
, pass xdocument's save() method, way can have more control on type of encoding used:
var doc = new xdocument( new xdeclaration("1.0", "utf-8", null), new xelement("root", new xattribute("note", "boogers")) ); using (var writer = new xmltextwriter(".\\boogers.xml", new utf8encoding(false))) { doc.save(writer); }
the utf8encoding
class constructor has overload specifies whether or not use bom (byte order mark) boolean value, in case false
.
the result of code verified using notepad++ inspect file's encoding.
Comments
Post a Comment