XDocument: saving XML to file without BOM

后端 未结 3 1236
眼角桃花
眼角桃花 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)

    0 讨论(0)
  • 2020-11-27 19:58

    Use an XmlTextWriter and pass that to the XDocument's Save() method, that way you can have more control over the 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 an overload that specifies whether or not to use the BOM (Byte Order Mark) with a boolean value, in your case false.

    The result of this code was verified using Notepad++ to inspect the file's encoding.

    0 讨论(0)
  • 2020-11-27 20:06

    The most expedient way to get rid of the BOM character when using XDocument is to just save the document, then do a straight File read as a file, then write it back out. The File routines will strip the character out for you:

            XDocument xTasks = new XDocument();
            XElement xRoot = new XElement("tasklist",
                new XAttribute("timestamp",lastUpdated),
                new XElement("lasttask",lastTask)
            );
            ...
            xTasks.Add(xRoot);
            xTasks.Save("tasks.xml");
    
            // read it straight in, write it straight back out. Done.
            string[] lines = File.ReadAllLines("tasks.xml");
            File.WriteAllLines("tasks.xml",lines);
    

    (it's hoky, but it works for the sake of expediency - at least you'll have a well-formed file to upload to your online provider) ;)

    0 讨论(0)
提交回复
热议问题