473,386 Members | 1,962 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 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 18920
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Hardy Wang | last post by:
Hi, I have following code: Encoding mode; // Encoding.Default or Encoding.UTF8 FileStream sb = new FileStream(fullPathAndFileName, FileMode.Create); StreamWriter sw = new StreamWriter(sb, mode);...
1
by: Frank Esser | last post by:
Hello! On a PC with German Codepage settings I want to get UTF8 out of string in my application. I use this function: Byte array = Encoding.UTF8.GetBytes("à"); When I look at the Unicode...
11
by: beachboy | last post by:
Hello all, I am building a CMS which has 2 language: English & Traditional Chinese my problem is all data are represent as "?????????", all pagecode are set to utf8 do I need to encoding(->...
7
by: EmeraldShield | last post by:
We have an application that uses UTF8 everywhere to load / save / process documents. One of our clients is having a problem with BIG Encoded files being trashed after running through our app. ...
3
by: Matt | last post by:
I have a problem where I am working with extended character sets in XML but I have also found that any time I work with a translation or internally generated Xml document I get the dreaded message,...
1
by: Heron | last post by:
Hi, I'm having a problem deserializing my streams since they are utf8 encoded (they are being received over tcp/ip) so I was looking for a way to make the serializer use utf8, is there any? ...
0
by: Daniel | last post by:
how does ado.net SqlDataReader.GetString() know which encoding to read the data into a string as? Does sql sever set this at the column data type level, server wide encoding setting, os encoding?
0
YuriyRusinov
by: YuriyRusinov | last post by:
Hello ! I have database linkdb with UTF8 encoding. Script \encoding WIN1251 begin; \i ./database_schema.sql
1
by: Tejas | last post by:
Hi, I am using ldap_get_values() call to get the user attributes from LDAP. This call is returning the user attributes in UTF-8 encoding and its a PCHAR*. For normal English characters this...
0
by: damonwischik | last post by:
I use emacs 22 and python-mode. Emacs can display utf8 characters (e.g. when I open a utf8-encoded file with Chinese, those characters show up fine), and I'd like to see utf8-encoded output from my...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.