Bug 8637 - XDocument doesn't honor XDeclaration.Encoding
Summary: XDocument doesn't honor XDeclaration.Encoding
Alias: None
Product: Class Libraries
Classification: Mono
Component: System.XML (show other bugs)
Version: unspecified
Hardware: PC Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Atsushi Eno
Depends on:
Reported: 2012-11-28 11:29 UTC by Jérémie Laval
Modified: 2013-08-09 02:58 UTC (History)
2 users (show)

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


Description Jérémie Laval 2012-11-28 11:29:25 UTC
csharp> new XDocument (new XDeclaration ("1.0", "utf-8", "yes")).ToString ();
"<?xml version=\"1.0\" encoding=\"utf-16\" standalone=\"yes\"?>"

In that case I should have had a "utf-8" encoding instead of "utf-16". Code never access the .Encoding property of an existing xmlDecl.
Comment 1 Atsushi Eno 2012-11-29 01:06:23 UTC
Hmm, first we need to examine if this writes encoding="utf-8" on .NET.

csharp> using System.Xml;
csharp> var w = new XmlWriterSettings () { Encoding = System.Text.Encoding.UTF8 }; 
csharp> using System.IO;    
csharp> var xw = XmlWriter.Create (new StringWriter (), w);
csharp> xw.WriteStartDocument ();
csharp> xw.WriteStartElement ("root");
csharp> xw.Close ();
csharp> var sw = new StringWriter ();
csharp> var xw = XmlWriter.Create (sw, w);                  
csharp> xw.WriteStartDocument ();          
csharp> xw.WriteStartElement ("root");                      
csharp> xw.Close ();
csharp> sw
<?xml version="1.0" encoding="utf-16"?><root />
Comment 2 Atsushi Eno 2012-11-29 04:03:25 UTC
The assumption that utf-8 should be output, is wrong. .NET also outputs utf-16. Though there is another ToString() behavior difference - .NET never outputs XML declaration at all. We rather need to fix it by doing so.


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

public class Test
	public static void Main ()
		var w = new XmlWriterSettings () { Encoding = Encoding.UTF8 };
		var sw = new StringWriter ();
		var xw = XmlWriter.Create (sw, w);
		xw.WriteStartDocument ();
		xw.WriteStartElement ("root");
		xw.Close ();
		Console.WriteLine (sw);

		Console.WriteLine ("-----------");

		var x = new XDocument (new XDeclaration ("1.0", "utf-8", "no"));
		x.Add (new XElement ("root"));
		Console.WriteLine (x.ToString ());
		Console.WriteLine ("-----------");
		var sw2 = new StringWriter ();
		x.Save (sw2);
		Console.WriteLine (sw2);

- .NET output -

$ ./test.exe
<?xml version="1.0" encoding="utf-16"?><root />
<root />
<?xml version="1.0" encoding="utf-16" standalone="no"?>
<root />

- Mono output -

$ mono ./test.exe

<?xml version="1.0" encoding="utf-16"?><root />
<?xml version="1.0" encoding="utf-16" standalone="no"?>
<root />
<?xml version="1.0" encoding="utf-16" standalone="no"?>
<root />
Comment 3 Jérémie Laval 2012-11-29 05:54:06 UTC
I confirm that on .NET:

-> new XDocument (new XDeclaration ("1.0", "utf-8", "yes")).ToString () returns string.Empty;

-> 		var xdoc = new XDocument (new XDeclaration ("1.0", "utf-8", "true"));
		xdoc.Add (new XElement ("root", new XAttribute ("hey", "heeeeeyaaaaaaa")));
		var writer = new StringWriter ();
		xdoc.Save (writer);
		Console.WriteLine (writer.ToString ());
Has a utf-16 encoding XML declaration.

So it seems like .NET is broken too. Not output'ing the declaration with ToString works for me.
Comment 4 Mateusz Mor 2013-07-11 06:13:27 UTC
I made a simple fix to this problem. You can find it over here:

Comment 5 Atsushi Eno 2013-08-09 02:58:32 UTC
oops, sorry I thought I fixed this bug. Now the fix is in git master [ccab578]

Note You need to log in before you can comment on or make changes to this bug.