473,396 Members | 1,940 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,396 software developers and data experts.

XML serialization and UTF encoding 8


I am trying to generate XML file with UTF encoding 8 ,
But after serliazation the xml is generated as
<?xml version=\"1.0\" encoding=\"utf-16\"?>
I want UTF encoding 8 .

How can I achieve that.

The current Code is
Expand|Select|Wrap|Line Numbers
  1. IDA idaEntity = new IDA ();
  2. idaEntity.First_Name ="abc";
  3. idaEntity.Last_Name ="def";
  4. idaEntity.Address_Line1 ="123456";
  5. idaEntity.Address_Line2 ="";
  6. idaEntity.City ="abcdef";
  7. idaEntity.State ="AA";
  8. idaEntity.Zip_Code ="12345";
  9. idaEntity.Home_Phone ="1234567890";
  10. idaEntity.Work_Phone  ="";
  11. idaEntity.SSN ="123456789";
  12. idaEntity.Customer_ID = "1234567";
  13. idaEntity.Order_ID= "98765432";
  14.  
  15. StringBuilder sb = new  StringBuilder  ();
  16.  
  17. XmlSerializer addSerializer = new XmlSerializer(typeof(IDA));
  18.  
  19. XmlTextWriter writer = new   XmlTextWriter( new StringWriter (sb) );
  20. writer.Formatting = Formatting.None;
  21. addSerializer.Serialize(writer, idaEntity);
  22. Console.WriteLine(sb);
  23.  
  24.  
The output I am getting is

[OUTPUT]
<?xml version="1.0" encoding="utf-16"?><IDA
xmlns:xsd="http://www.w3.org/2001/XM
LSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Customer_ID>50027961</Customer_ID><Order_ID>50027317</Order_ID><First_Name>abc
</First_Name><Last_Name>def</Last_Name><Address_Line1>123456</Address_Line1><Ad
dress_Line2
/><City>abcdef</City><State>AA</State><Zip_Code>12345</Zip_Code><
SSN>123456789</SSN><Home_Phone>1234567890</Home_Phone><Work_Phone
/></IDA>

[/OUTPUT]

How can I get encoding utf-8 instead of 16.

Thanks !

Dec 5 '06 #1
3 19966
piku wrote:
I am trying to generate XML file with UTF encoding 8 ,
But after serliazation the xml is generated as
<?xml version=\"1.0\" encoding=\"utf-16\"?>
I want UTF encoding 8 .

How can I achieve that.
Serialize to a file or a stream, not to a string writer as a string is
always (internally) UTF-16 encoded.
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Dec 5 '06 #2
Martin Honnen <ma*******@yahoo.dewrote:
piku wrote:
I am trying to generate XML file with UTF encoding 8 ,
But after serliazation the xml is generated as
<?xml version=\"1.0\" encoding=\"utf-16\"?>
I want UTF encoding 8 .

How can I achieve that.

Serialize to a file or a stream, not to a string writer as a string is
always (internally) UTF-16 encoded.
However, you can still make a StringWriter expose its encoding as UTF-8
by overriding its properties.

It so happens I have a class which does exactly that in my utility
library :)
See http://www.pobox.com/~skeet/csharp/miscutil
and look at MiscUtil.IO.StringWriterWithEncoding

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 5 '06 #3

Use a MemoryStream and StreamWriter and in the constructor to the
StreamWriter you can specify the encoding to use.

HTH,

Sam
------------------------------------------------------------
We're hiring! B-Line Medical is seeking Mid/Sr. .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.
On 5 Dec 2006 06:19:52 -0800, "piku" <pu****@gmail.comwrote:
>
I am trying to generate XML file with UTF encoding 8 ,
But after serliazation the xml is generated as
<?xml version=\"1.0\" encoding=\"utf-16\"?>
I want UTF encoding 8 .

StringBuilder sb = new StringBuilder ();

XmlSerializer addSerializer = new XmlSerializer(typeof(IDA));
Dec 5 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Mark | last post by:
Hello, What I am trying todo is use .NET serialization to take a Guid property that is an attribute in the XML document and have its output to be the same as...
4
by: Jeff T. | last post by:
Hello, I have an existing set of C# classes that encapsulate our application data. They are in a heirachy with each subclass defining more specific types of data. I would like to serialize these...
0
by: psy000 | last post by:
Hi, I have a C# web service client that talks to a JAVA application sever. I use AXIS to generate the WSDL file, use wsdl.exe to generate proxy stub c# code. When I try to use c# client connect...
4
by: Brian Keating | last post by:
wonder if anyone can help me here, i've a framework 1.1 dataset which i serialize in framework 1.1 and deserialize in framework 2.0. This is fine, problem is that i want to modify some of the...
0
by: umhlali | last post by:
I get the following exception when my VB.NET app calls a Java web service that returns an array of objects. The same call works for a single object though. So looks like there is no problem...
0
by: Chris McDonough | last post by:
ElementTree's XML serialization routine implied by tree._write(file, node, encoding, namespaces looks like this (elided): def _write(self, file, node, encoding, namespaces): # write XML to file...
4
by: Sjaakie | last post by:
Hi, I've read some articles on serialization, but I don't see how to serialize the xml-structure below (if it is possible at all). Could you point me in the right direction? TIA <?xml...
4
by: Val | last post by:
I have a complex object that I need to serialize. Rather than rely on a standard routine, which is called during the serialization/deserialization, I would like to be able to use my own functions...
1
by: Sarika Agarwal | last post by:
Hi, What is the primary difference between serialization and encoding in ..NET! *** Sent via Developersdex http://www.developersdex.com ***
2
by: Nishant Mehta | last post by:
Hi all, I am serialiizing a c# object to an XML file to save some application settings. The idea is to ship this xml file along with the application and deserialize after the application is...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.