Different xml representations

M

Martin

Hi,

I would like to ask to ask why when I output some xml to the console window
i get different encodings.
for example, consider the following code.

XmlDocument xmldoc = new XmlDocument();

xmldoc.LoadXml(@"<?xml version=""1.0"" encoding=""utf-16""?>

<Vehicle xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance""
xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">

<VIN>VIN</VIN>

<Make>Make</Make>

<Model>Model</Model>

</Vehicle>");

when i run this in a console window and execute the following line

xmldoc.Save(Console.Out);

i get the following xml - notice that the encoding is "IBM437" and not
"UTF-16" as originally specified

<?xml version="1.0" encoding="IBM437"?>
<Vehicle xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http:/
/www.w3.org/2001/XMLSchema">
<VIN>VIN</VIN>
<Make>Make</Make>
<Model>Model</Model>
</Vehicle>

however when I used the following line

Console.WriteLine(xmldoc.OuterXml);

The it appears in the console window with the correct formating.

can anybody please explain why this is.

many thanks in advance.

cheers

martin.
 
J

Jon Skeet [C# MVP]

The it appears in the console window with the correct formating.
can anybody please explain why this is.

Sure. XmlDocument.Save(TextWriter) is using the encoding of the text
writer. In the case of Console.Out, that encoding is IBM code page 437.
When you're just using xmldoc.OuterXml it's using the "natural"
encoding of the document.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top