我試圖用一些轉義字符(\ 0)序列化一個字符串。但是,當我反序列化它會引發異常。爲什麼XML序列化建議通過SOAP和二進制序列化?
//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());
}
@ starz26 - 什麼樣的異常? – CoderHawk 2010-08-12 06:49:12
你的問題描述有哪些主題? – Gopi 2010-08-12 06:50:01
因爲這個異常我試圖使用其他序列化類型。可以使用二進制嗎? – myWorld 2010-08-12 08:39:02