我正在尝试使用一些转义字符(\ 0)序列化字符串。但是当我反序列化它会引发异常。
//XML serialization
private static void M1()
{
string str = "\0AC";
StringWriter sw = new StringWriter();
XmlSerializer serializer = new XmlSerializer(typeof(String));
serializer.Serialize(sw, str);
System.Console.WriteLine("String encoded to XML = \n{0} \n", sw.ToString());
StringReader sr = new StringReader(sw.ToString());
String s2 = (String)serializer.Deserialize(sr);
System.Console.WriteLine("String decoded from XML = \n {0}", s2);
}
//Using Binary serialization
void BinaryFormat1()
{
IFormatter bf = new BinaryFormatter();
string str = "\0MyName";
FileStream fs = new FileStream("SerilizedString.Data", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
bf.Serialize(fs, str);
fs.Close();
fs = new FileStream("SerilizedString.Data", FileMode.Open, FileAccess.Read, FileShare.None);
string data = (string)bf.Deserialize(fs);
fs.Close();
Console.WriteLine("data : " + data.ToString());
}
答案 0 :(得分:3)
老实说,我希望SOAP有同样的问题。这是无法用“标准”XML表示每个Unicode字符串的问题。
留下二进制序列化,其中:
就个人而言,我更喜欢自定义序列化选项,如Thrift或Protocol Buffers,但作为C#的Protocol Buffers端口的作者,我有偏见:)