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

XmlSerializiation to string using utf-8

I have the following utility function to serialize my object to XML

public static string WriteToString( object o, Type type )
{
XmlSerializer serializer = new XmlSerializer( type );
StringWriter output = new StringWriter( );

serializer.Serialize(output, o );

return output.ToString( );
}

This method works great, except for one issue. Because the underlying
data is a string (unicode), the serialization process automatically
writes <?xml version='1.0' encoding='utf-16'?> out and there is no way
with strings of changing this encoding type.

Now if I was writing to a file, I would have no issues as StreamWriter
which is used for files supports an Encoding enumeration. But I don't
want to write the xml directly to a SqlServer stored procedure, but
the SP fails because it wants to see utf-8, now if I manually the
utf-16 to utf-8 (I know that the underlying data really is utf-16),
then SqlServer has no problems.

I want to find out how I can serialize an object to string, but change
the encoding type.

Cheers Dave
Nov 16 '05 #1
2 1940
muesliflakes,

You can't do this. The reason for this is that strings are always
stored in unicode. If you want to convert to UTF8, then you have to use the
UTF8Encoding class and call the GetBytes method to get the byte
representation of the string. Of course, you run the risk of dropping data,
since not everything that is represented in unicode can be represented in
UTF8.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"muesliflakes" <mu**********@yahoo.com.au> wrote in message
news:8d**************************@posting.google.c om...
I have the following utility function to serialize my object to XML

public static string WriteToString( object o, Type type )
{
XmlSerializer serializer = new XmlSerializer( type );
StringWriter output = new StringWriter( );

serializer.Serialize(output, o );

return output.ToString( );
}

This method works great, except for one issue. Because the underlying
data is a string (unicode), the serialization process automatically
writes <?xml version='1.0' encoding='utf-16'?> out and there is no way
with strings of changing this encoding type.

Now if I was writing to a file, I would have no issues as StreamWriter
which is used for files supports an Encoding enumeration. But I don't
want to write the xml directly to a SqlServer stored procedure, but
the SP fails because it wants to see utf-8, now if I manually the
utf-16 to utf-8 (I know that the underlying data really is utf-16),
then SqlServer has no problems.

I want to find out how I can serialize an object to string, but change
the encoding type.

Cheers Dave

Nov 16 '05 #2
I should correct myself. UTF-8 -> Unicode is lossless.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:un**************@TK2MSFTNGP12.phx.gbl...
muesliflakes,

You can't do this. The reason for this is that strings are always
stored in unicode. If you want to convert to UTF8, then you have to use the UTF8Encoding class and call the GetBytes method to get the byte
representation of the string. Of course, you run the risk of dropping data, since not everything that is represented in unicode can be represented in
UTF8.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"muesliflakes" <mu**********@yahoo.com.au> wrote in message
news:8d**************************@posting.google.c om...
I have the following utility function to serialize my object to XML

public static string WriteToString( object o, Type type )
{
XmlSerializer serializer = new XmlSerializer( type );
StringWriter output = new StringWriter( );

serializer.Serialize(output, o );

return output.ToString( );
}

This method works great, except for one issue. Because the underlying
data is a string (unicode), the serialization process automatically
writes <?xml version='1.0' encoding='utf-16'?> out and there is no way
with strings of changing this encoding type.

Now if I was writing to a file, I would have no issues as StreamWriter
which is used for files supports an Encoding enumeration. But I don't
want to write the xml directly to a SqlServer stored procedure, but
the SP fails because it wants to see utf-8, now if I manually the
utf-16 to utf-8 (I know that the underlying data really is utf-16),
then SqlServer has no problems.

I want to find out how I can serialize an object to string, but change
the encoding type.

Cheers Dave


Nov 16 '05 #3

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

Similar topics

3
by: Michael Weir | last post by:
I'm sure this is a very simple thing to do, once you know how to do it, but I am having no fun at all trying to write utf-8 strings to a unicode file. Does anyone have a couple of lines of code...
8
by: Eric Lilja | last post by:
Hello, I had what I thought was normal text-file and I needed to locate a string matching a certain pattern in that file and, if found, replace that string. I thought this would be simple but I had...
1
by: Neil Schemenauer | last post by:
The title is perhaps a little too grandiose but it's the best I could think of. The change is really not large. Personally, I would be happy enough if only %s was changed and the built-in was...
23
by: Steven T. Hatton | last post by:
This is one of the first obstacles I encountered when getting started with C++. I found that everybody had their own idea of what a string is. There was std::string, QString, xercesc::XMLString,...
5
by: Mike Currie | last post by:
Can anyone explain why I'm getting an ascii encoding error when I'm trying to write out using a UTF-8 encoder? Thanks Python 2.4.3 (#69, Mar 29 2006, 17:35:34) on win32 Type "help",...
1
by: sheldon.regular | last post by:
I am new to unicode so please bear with my stupidity. I am doing the following in a Python IDE called Wing with Python 23. äöü äöü '\xc3\xa4\xc3\xb6\xc3\xbc' u'\xe4\xf6\xfc'...
1
by: amandeep.bhatia1 | last post by:
Hello Friends, I am working on a project to support internationalization for a existing project. While supporting UTF-8 I am facing a problem , while doing POC. I have a C string which I...
23
by: Allan Ebdrup | last post by:
I hava an ajax web application where i hvae problems with UTF-8 encoding oc chineese chars. My Ajax webapplication runs in a HTML page that is UTF-8 Encoded. I copy and paste some chineese chars...
4
by: J Peyret | last post by:
Well, as usual I am confused by unicode encoding errors. I have a string with problematic characters in it which I'd like to put into a postgresql table. That results in a postgresql error so I...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.