Xml Encoding Problem!!!

V

Vai2000

Hi All, I have an xml document which gets generated out of a process with
utf-16 type of encoding. I am writing this to a file. How can I convert it
to utf-8 since IE won't open the Document.
Tried using Encoding.Unicode,Encoding.Utf8 flags in the StreamWriter but no
luck!!!

// this is the code I am using to write out
StreamWriter sw=new StreamWriter(fileOut);
sw.Write(content,Encoding.Unicode);
//sw.Write(content,Encoding.Utf8);
sw.Close();
TIA
 
J

Jon Skeet [C# MVP]

Vai2000 said:
Hi All, I have an xml document which gets generated out of a process with
utf-16 type of encoding. I am writing this to a file. How can I convert it
to utf-8 since IE won't open the Document.
Tried using Encoding.Unicode,Encoding.Utf8 flags in the StreamWriter but no
luck!!!

// this is the code I am using to write out
StreamWriter sw=new StreamWriter(fileOut);
sw.Write(content,Encoding.Unicode);
//sw.Write(content,Encoding.Utf8);
sw.Close();

Not sure what you're doing in that code - the StreamWriter should have
a UTF-8 encoding by default.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
V

Vai2000

basically I get a xml doc which has encoding of UTF-16.
I want to write out this XML in a file with encoding of UTF-8

How do I accomplish it?
 
M

Mickey Williams

By default, the XmlTextWriter instance embedded inside the serializer will
write with UTF-16 encoding. If you want different encoding (or other
non-default behavior), you must create an instance of XmlTextWriter, and use
it when serializing, like this:

static void SerializeThingToFile(string path, object thing)
{
XmlTextWriter writer = new XmlTextWriter(path, Encoding.UTF8);
XmlSerializer serializer = new XmlSerializer(thing.GetType());
serializer.Serialize(writer, thing);
}

--
Mickey Williams
Author, "Microsoft Visual C# .NET Core Reference", MS Press
www.servergeek.com/blogs/mickey


Vai2000 said:
basically I get a xml doc which has encoding of UTF-16.
I want to write out this XML in a file with encoding of UTF-8

How do I accomplish it?

convert
but
 
J

Jon Skeet [C# MVP]

Vai2000 said:
basically I get a xml doc which has encoding of UTF-16.

By the time you've loaded the XML document, it really doesn't *have* an
encoding any more, I believe.
I want to write out this XML in a file with encoding of UTF-8

How do I accomplish it?

Use XmlDocument.Save with a TextWriter of some description which uses a
UTF-8 encoding.
 
V

Vai2000

XmlDocument doc=new XmlDocument();


// fails here!!!!"There is no Unicode byte order mark. Cannot switch to
Unicode."
doc.Load(@"C:\XML\report.xml");


XmlTextWriter writer = new
XmlTextWriter(@"C:\XMl\Unicode.xml",Encoding.Unicode);

writer.Formatting = Formatting.Indented;

doc.WriteTo( writer );

writer.Flush();
 
J

Jon Skeet [C# MVP]

Vai2000 said:
XmlDocument doc=new XmlDocument();
// fails here!!!!"There is no Unicode byte order mark. Cannot switch to
Unicode."
doc.Load(@"C:\XML\report.xml");

Ah. You'd have saved a fair amount of speculation if you'd said that
you haven't even managed to load the document yet.

You can probably get away with

using (StreamReader reader = new StreamReader(@"C:\XML\report.xml",
Encoding.Unicode))
{
doc.Load(reader);
}
 

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

Similar Threads

aps.net : BIG BUG in streamwriter 17
Encoding.Default and Encoding.UTF8 5
XML file and UTF8 4
how to send xml file 7
got totally confused about the netstream 7
XmlSerializer again 3
encoding 1
XML HTTP 10

Top