472,143 Members | 1,421 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,143 software developers and data experts.

XML is UTF-16 encoding but I want UTF-8 encoding ?

Hi there XML Gurus ;)

I am trying to use XML Serialization to create a xml from a class, this is the output which I get when I create the XML and put in a string variale.

"<?xml version=\"1.0\" encoding=\"utf-16\"?><OutBS><FirstName>XML</FirstName><LastName>Guru</LastName><MemberName>gu**@xml.com</MemberName></OutBS>"

My question is why am I getting the encoding as UTF-16, I want a UTF-8 encoding, how can this be achieve by getting the output in the xout string variable.

Help is really appreciated.
thanks,
shailendra batham

// Create the xout xml here.
OutBS obs = new OutBS();
obs.FirstName = "XML";
obs.LastName = "Guru";
obs.MemberName = "gu**@xml.com";

StringWriter sw = new StringWriter();
XmlSerializer xs = new XmlSerializer(obs.GetType());
xs.Serialize(sw, obs);
String xout = sw.ToString();

public class OutBS
{
public String FirstName;
public String LastName;
public String MemberName;
}

--------------------------------------------------------------------------------

Nov 12 '05 #1
2 18688
StringWriter can only produce UTF-16 by definition.
If you want some other encoding use StreamWriter.
"Shailendra Batham" <sg******@sbcglobal.net> wrote in message news:vm******************@newssvr21.news.prodigy.c om...
Hi there XML Gurus ;)

I am trying to use XML Serialization to create a xml from a class, this is the output which I get when I create the XML and put in a string variale.

"<?xml version=\"1.0\" encoding=\"utf-16\"?><OutBS><FirstName>XML</FirstName><LastName>Guru</LastName><MemberName>gu**@xml.com</MemberName></OutBS>"

My question is why am I getting the encoding as UTF-16, I want a UTF-8 encoding, how can this be achieve by getting the output in the xout string variable.

Help is really appreciated.
thanks,
shailendra batham

// Create the xout xml here.
OutBS obs = new OutBS();
obs.FirstName = "XML";
obs.LastName = "Guru";
obs.MemberName = "gu**@xml.com";

StringWriter sw = new StringWriter();
XmlSerializer xs = new XmlSerializer(obs.GetType());
xs.Serialize(sw, obs);
String xout = sw.ToString();

public class OutBS
{
public String FirstName;
public String LastName;
public String MemberName;
}

------------------------------------------------------------------------------

Nov 12 '05 #2
"Shailendra Batham" <sg******@sbcglobal.net> wrote in message news:vm******************@newssvr21.news.prodigy.c om...
My question is why am I getting the encoding as UTF-16, I want a
UTF-8 encoding, how can this be achieve by getting the output in
the xout string variable.


Shailendra,

In .NET, a String is a sequence of zero or more Chars, and each
Char is a two-byte Unicode character. Therefore, a String will
always be UTF-16, and can be no other encoding.

If you really care about getting UTF-8 encoding, then instead of
serializing to a StringWriter you should serialize to a Memory-
Stream wrapping a byte[]. You'd have decode the byte[] into
UTF-16 to read it naturally, but the byte[] would contain UTF-8
encoded data.

- - - Utf8Serialize.cs (excerpt)
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
// . . .
MemoryStream memStrm = new MemoryStream( );
XmlTextWriter xmlSink = new XmlTextWriter( memStrm, Encoding.UTF8);
xs.Serialize( xmlSink, ob);

byte[] utf8EncodedData = memStrm.GetBuffer( );
// . . .
- - -
Derek Harmon
Nov 12 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Hardy Wang | last post: by
1 post views Thread by Frank Esser | last post: by
11 posts views Thread by beachboy | last post: by
7 posts views Thread by EmeraldShield | last post: by
3 posts views Thread by Matt | last post: by
1 post views Thread by Heron | last post: by
reply views Thread by damonwischik | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.