Bug 4850 - XDocument.WriteTo() does not write <?xml?> preamble/header (.NET does)
Summary: XDocument.WriteTo() does not write <?xml?> preamble/header (.NET does)
Alias: None
Product: Class Libraries
Classification: Mono
Component: System.XML ()
Version: 2.10.x
Hardware: PC Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2012-05-02 19:26 UTC by Aaron Bockover
Modified: 2012-05-14 04:29 UTC (History)
2 users (show)

Is this bug a regression?: ---
Last known good build:

Test Case (560 bytes, application/octet-stream)
2012-05-02 19:26 UTC, Aaron Bockover

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Aaron Bockover 2012-05-02 19:26:27 UTC
Created attachment 1797 [details]
Test Case

The following test produces different documents on Mono vs .NET:

using System;
using System.Text;
using System.Xml;
using System.Xml.Linq;

public static class Entry
    public static void Main (string [] args)
        var doc = new XDocument (
            new XElement("resources",
                new XElement("string",
                    new XAttribute("name", "whatever"),
                    "This is sparta")

        using (var writer = new XmlTextWriter (args[0], Encoding.UTF8) {
            Formatting = Formatting.Indented }) {
            doc.WriteTo (writer);

Mono produces:

  <string name="whatever">This is sparta</string>

While .NET produces:

<?xml version="1.0" encoding="utf-8"?>
  <string name="whatever">This is sparta</string>

The only difference being the addition of the <?xml version="1.0" encoding="utf-8"?>

Note that if the encoding argument to the XmlTextWriter constructor is specified as null then no encoding attribute is written to the header.

Mono should match .NET.
Comment 1 Atsushi Eno 2012-05-14 04:19:32 UTC
It's not about XmlTextWriter but rather about XDocument.WriteTo().

I'm fixing the issue.
Comment 2 Atsushi Eno 2012-05-14 04:29:40 UTC
Fixed in git master and 2-10. Thanks.