XDocument: saving XML to file without BOM

后端 未结 3 1257
眼角桃花
眼角桃花 2020-11-27 19:43

I\'m generating an utf-8 XML file using XDocument.

XDocument xml_document = new XDocument(
                    new XDeclaration(\"1.0\"         


        
3条回答
  •  眼角桃花
    2020-11-27 19:46

    First of all: the service provider MUST handle it, according to XML spec, which states that BOM may be present in case of UTF-8 representation.

    You can force to save your XML without BOM like this:

    XmlWriterSettings settings = new XmlWriterSettings();
    settings.Encoding = new UTF8Encoding(false); // The false means, do not emit the BOM.
    using (XmlWriter w = XmlWriter.Create("my.xml", settings))
    {
        doc.Save(w);
    }
    

    (Googled from here: http://social.msdn.microsoft.com/Forums/en/xmlandnetfx/thread/ccc08c65-01d7-43c6-adf3-1fc70fdb026a)

提交回复
热议问题