Although a . Net string is always UTF-16 you need to serialize the object using UTF-16 encoding. That sould be something like this.
Although a . Net string is always UTF-16 you need to serialize the object using UTF-16 encoding. That sould be something like this: public static string ToString(object source, Type type, Encoding encoding) { // The string to hold the object content String content; // Create a memoryStream into which the data can be written and readed using (var stream = new MemoryStream()) { // Create the xml serializer, the serializer needs to know the type // of the object that will be serialized var xmlSerializer = new XmlSerializer(type); // Create a XmlTextWriter to write the xml object source, we are going // to define the encoding in the constructor using (var writer = new XmlTextWriter(stream, encoding)) { // Save the state of the object into the stream xmlSerializer.
Serialize(writer, source); // Flush the stream writer.Flush(); // Read the stream into a string using (var reader = new StreamReader(stream, encoding)) { // Set the stream position to the begin stream. Position = 0; // Read the stream into a string content = reader.ReadToEnd(); } } } // Return the xml string with the object content return content; } By setting the encoding to Encoding. Unicode not only the string will be UTF-16 but you should also get the xml string as UTF-16....
This is it. It's the most flexible one – veljkoz Sep 21 '10 at 14:05 Hmm, correct me if I'm wrong here, but all this code is doing is setting encoding="utf-16" in the top of the XML data. The content string is UTF-16 regardless of what encoding you use for your XmlTextWriter.
– Isak Savo Sep 21 '10 at 14:31 Yes precisely. It's not a question if the string is UTF-8 or UTF-16, as you said previously it's always UTF-16. The question is to set the encoding="utf-16" or "utf-8".
– Pedro Sep 21 '10 at 14:42.
A string is always UTF-16 in . NET, so as long as you stay inside your managed app you don't have to care about which encoding it is. The problem is more likely where you talk to the SQL server.
Your question doesn't show that code so it's hard to pin point the exact error. My suggestion is you check if there's a property or attribute you can set on that code that specifies the encoding of the data sent to the server.
You we're right - it seems that Sql was configured to accept only UTF-8 in xml columns. +1 – veljkoz Sep 21 '10 at 14:06.
You are serializing to a string rather than a byte array so, at this point, any encoding hasn't happened yet. What does the start of "messageToLog" look like? Is the XML specifying an encoding (e.g. Utf-8) which subsequently turns out to be wrong?
Edit Based on your further info it sounds like the string is automatically converted to utf-8 when it is passed to the database, but the database chokes because the XML declaration says it is utf-16. In which case, you don't need to serialize to utf-8. You need to serialize with the "encoding=" omitted from the XML.
The XmlFragmentWriter (not a standard part of . Net, Google it) lets you do this.
Default encoding for a xml serializer should be UTF-16. Just to make sure you can try - XmlSerializer serializer = new XmlSerializer(typeof(YourObject)); // create a MemoryStream here, we are just working // exclusively in memory System.IO. Stream stream = new System.IO.MemoryStream(); // The XmlTextWriter takes a stream and encoding // as one of its constructors System.Xml.
XmlTextWriter xtWriter = new System.Xml. XmlTextWriter(stream, Encoding. UTF16); serializer.
Serialize(xtWriter, yourObjectInstance); xtWriter.Flush().
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.