使用的StringWriter的XML序列化 [英] Using StringWriter for XML Serialization
问题描述
目前,我正在寻找一种简单的方法来序列化对象(在C#3)。
I'm currently searching for an easy way to serialize objects (in C# 3).
我GOOGLE了一些例子,喜欢的东西想出了:
I googled some examples and came up with something like:
MemoryStream memoryStream = new MemoryStream ( );
XmlSerializer xs = new XmlSerializer ( typeof ( MyObject) );
XmlTextWriter xmlTextWriter = new XmlTextWriter ( memoryStream, Encoding.UTF8 );
xs.Serialize ( xmlTextWriter, myObject);
string result = Encoding.UTF8.GetString(memoryStream .ToArray());
阅读本<经过href=\"http://stackoverflow.com/questions/1138414/can-i-serialize-xml-straight-to-a-string-instead-of-a-stream-with-c\">question我问自己,为什么不使用StringWriter的?这似乎更容易。
After reading this question I asked myself, why not using StringWriter? It seems much easier.
XmlSerializer ser = new XmlSerializer(typeof(MyObject));
StringWriter writer = new StringWriter();
ser.Serialize(writer, myObject);
serializedValue = writer.ToString();
另一个问题是,那第一个例子中生成的XML我不能只是写到SQL Server 2005的XML列DB。
Another Problem was, that the first example generated XML I could not just write into an XML column of SQL Server 2005 DB.
第一个问题是:是否有一个原因,我不应该使用StringWriter的序列化对象时,我需要它作为一个字符串,后来呢?我从来没有发现谷歌搜索时使用的StringWriter的结果。
The first question is: Is there a reason why I shouldn't use StringWriter to serialize an Object when I need it as a string afterwards? I never found a result using StringWriter when googling.
第二,当然是:如果你不应该StringWriter的做到这一点(不管是什么原因),这将是一个良好和正确的方法
The second is, of course: If you should not do it with StringWriter (for whatever reasons), which would be a good and correct way?
增加:
因为它已经被这两个答案中提到,我会进一步进入的XML DB的问题。
As it was already mentioned by both answers, I'll further go into the XML to DB problem.
当写入数据库,我得到了以下异常:
When writing to the Database I got the following exception:
System.Data.SqlClient.SqlException:
XML解析:1号线,38字符,
无法切换编码
System.Data.SqlClient.SqlException: XML parsing: line 1, character 38, unable to switch the encoding
对于字符串
<?xml version="1.0" encoding="utf-8"?><test/>
我把从XmlTextWriter对象创建的字符串,只是把为XML那里。这其中并没有(也没有用人工插入DB)工作。
I took the string created from the XmlTextWriter and just put as xml there. This one did not work (neither with manual insertion into the DB).
后来我试图手动插入与编码=UTF-16,这也失败了(只是写INSERT INTO ...)。
完全移除工作,然后编码。这一结果后,我切换回的StringWriter code和中提琴 - 它的工作。
Afterwards I tried manual insertion (just writing INSERT INTO ... ) with encoding="utf-16" which also failed. Removing the encoding totally worked then. After that result I switched back to the StringWriter code and viola - it worked.
问题:我真的不明白,为什么
Problem: I don't really understand why.
在基督教艾泰:有了这些测试,我不知道,我必须使用UTF-16写入到数据库。不会设置编码为UTF-16(在XML标签)工作呢?
at Christian Hayter: With those tests I'm not sure that I have to use utf-16 to write to the DB. Wouldn't setting the encoding to UTF-16 (in the xml tag) work then?
推荐答案
在连载的XML文档到一个.NET字符串,编码必须设置为UTF-16。字符串在内部存储为UTF-16,所以这是唯一有意义的编码。如果你想将数据存储在不同的编码,使用一个字节数组来代替。
When serialising an XML document to a .NET string, the encoding must be set to UTF-16. Strings are stored as UTF-16 internally, so this is the only encoding that makes sense. If you want to store data in a different encoding, you use a byte array instead.
SQL Server的工作在一个类似的原则;传递到一个 XML任何字符串
列必须是连接codeD为UTF-16。 SQL Server将拒绝那些XML声明没有指定UTF-16的字符串。如果XML声明不是present,那么XML标准要求,它默认为UTF-8,因此SQL Server将拒绝为好。
SQL Server works on a similar principle; any string passed into an xml
column must be encoded as UTF-16. SQL Server will reject any string where the XML declaration does not specify UTF-16. If the XML declaration is not present, then the XML standard requires that it default to UTF-8, so SQL Server will reject that as well.
考虑到这一点,这里有做转换一些实用的方法。
Bearing this in mind, here are some utility methods for doing the conversion.
public static string Serialize<T>(T value) {
if(value == null) {
return null;
}
XmlSerializer serializer = new XmlSerializer(typeof(T));
XmlWriterSettings settings = new XmlWriterSettings();
settings.Encoding = new UnicodeEncoding(false, false); // no BOM in a .NET string
settings.Indent = false;
settings.OmitXmlDeclaration = false;
using(StringWriter textWriter = new StringWriter()) {
using(XmlWriter xmlWriter = XmlWriter.Create(textWriter, settings)) {
serializer.Serialize(xmlWriter, value);
}
return textWriter.ToString();
}
}
public static T Deserialize<T>(string xml) {
if(string.IsNullOrEmpty(xml)) {
return default(T);
}
XmlSerializer serializer = new XmlSerializer(typeof(T));
XmlReaderSettings settings = new XmlReaderSettings();
// No settings need modifying here
using(StringReader textReader = new StringReader(xml)) {
using(XmlReader xmlReader = XmlReader.Create(textReader, settings)) {
return (T) serializer.Deserialize(xmlReader);
}
}
}
这篇关于使用的StringWriter的XML序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!