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

Serializing object to XML

Below is some code we have for converting our objects to XML. Both convert
the XML just fine, but I have a question about a small differnance.

The first block of code outputs the ?xm tag with utf-16, the second block
outputs it with utf-8. Why do they generate the header differnt? And why
would IE complain about the utf-16 file with the following error:

Switch from current encoding to specified encoding not supported.

Thanks
Wayne

string fileName = @"C:\Temp.xml";
StreamWriter sr = File.CreateText(fileName);
XmlSerializer serializer = new
XmlSerializer(typeof(USERINSPECTIONLETTERS));
TextWriter textwriter = new StringWriter();
serializer.Serialize(textwriter, letters);
sr.WriteLine(textwriter.ToString());
sr.Close();

XmlSerializer serializer2 = new
XmlSerializer(typeof(USERINSPECTIONLETTERS));
TextWriter writer = new StreamWriter(@"C:\Temp2.xml");
serializer2.Serialize(writer, letters);
writer.Close();
Nov 12 '05 #1
2 3694
"Wayne" <Me******@community.nospam> wrote in message news:Oz**************@TK2MSFTNGP09.phx.gbl...
The first block of code outputs the ?xm tag with utf-16, the second block
outputs it with utf-8. Why do they generate the header differnt? : : TextWriter textwriter = new StringWriter();
serializer.Serialize(textwriter, letters); : : TextWriter writer = new StreamWriter(@"C:\Temp2.xml");
serializer2.Serialize(writer, letters);


Strings are always UTF-16 encoded in .NET (two bytes per character), that's why the first
block emits the encoding as being UTF-16.

Otherwise, XML serialization defaults to UTF-8, as exhibited by the second block of code.
Derek Harmon
Nov 12 '05 #2
Thank you
Wayne

"Derek Harmon" <lo*******@msn.com> wrote in message
news:uw**************@tk2msftngp13.phx.gbl...
"Wayne" <Me******@community.nospam> wrote in message news:Oz**************@TK2MSFTNGP09.phx.gbl...
The first block of code outputs the ?xm tag with utf-16, the second block outputs it with utf-8. Why do they generate the header differnt?

: :
TextWriter textwriter = new StringWriter();
serializer.Serialize(textwriter, letters);

: :
TextWriter writer = new StreamWriter(@"C:\Temp2.xml");
serializer2.Serialize(writer, letters);


Strings are always UTF-16 encoded in .NET (two bytes per character),

that's why the first block emits the encoding as being UTF-16.

Otherwise, XML serialization defaults to UTF-8, as exhibited by the second block of code.

Derek Harmon

Nov 12 '05 #3

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

Similar topics

0
by: Ante Smolcic | last post by:
Hi all, I have an ArrayList that contains items of type A. I declared the XmlArrayItem atribute for that type. Now I have an derived type B (from A) also contained in the ArrayList but I get...
1
by: Ivo Bronsveld | last post by:
All, I have quite a challenging task ahead of me. I need to write an object model (for code access) based on a schema, which cannot be made into a dataset because of it's complexity. So I...
6
by: Rein Petersen | last post by:
Hi Folks! Here's a strange behaviour: Without a properties SET accessor (see code below), the property will not serialize. public class myObject {
1
by: Chris | last post by:
I'm having trouble Serializing a System.Data.DataColumn object. When I try to serialize it, I get the following: System.NotSupportedException: Cannot serialize member...
2
by: Tobias Zimmergren | last post by:
Hi, just wondering what serializing really is, and howto use it? Thanks. Tobias __________________________________________________________________ Tobias ICQ#: 55986339 Current ICQ status: +...
4
by: Dave Veeneman | last post by:
When does serializing objects make more sense than persisting them to a database? I'm new to object serialization, and I'm trying to get a feel for when to use it. Here is an example: I'm...
4
by: Jason Shohet | last post by:
We are thinking of serializing an object & passing it toseveral functions on web service. This will happen about 35 times as the page loads. The class has about 20 attributes. We're not sure...
3
by: axr | last post by:
Having trouble with Serilization of objects that contain members which are of type Interface eg public class SomeClass { ISomeInterface1 itf1; ClassType1 ct1; ISomeInterface2 itf2;
4
by: mookid8000 | last post by:
Good day group! I have created a nice filtering plugin system, where all filters derive from a Filter class, and they pass a PictureData object between them. I have a problem though. I am able...
2
by: Charles Law | last post by:
I have a complex object that I am serializing, and I would like to omit elements that have a default value. For example, if I have a class as follows Public Class Test Private m_Name As...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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.