/// 将一个ArrayList序列化成xml字符串
/// </summary>
/// <param name="list"></param>
/// <returns></returns>
private byte[] myXmlSerializer(ArrayList list)
{
byte[] array = null;
try
{
XmlSerializer ser = new XmlSerializer(typeof(Operate.Entity));
MemoryStream mem = new MemoryStream();
XmlTextWriter writer = new XmlTextWriter(mem, Encoding.Default);
foreach(object obj in list)
{
ser.Serialize(mem,obj);
}
array = new byte[mem.Length];
mem.Position = 0;
mem.Read(array, 0, (int)mem.Length);
mem.Close();
writer.Close();
}
catch(Exception ex)
{
txtState.Text = "序列化时出现错误:" + ex.ToString() + "\r\n" + txtState.Text;
return null;
}
return array;
}
/// <summary>
/// 将一个xml字符串反序列化成ArrayList
/// </summary>
/// <param name="s"></param>
/// <param name="t"></param>
/// <returns></returns>
private ArrayList myXmlDeserialize(byte[] bytes)
{
ArrayList list = null;
try
{
XmlSerializer mySerializer = new XmlSerializer(typeof(ServerOperate.Entity));
StreamReader mem = new StreamReader(new MemoryStream(bytes), System.Text.Encoding.Default);
//while (stream.Position != stream.Length)
//{
list.Add(mySerializer.Deserialize(mem));
//}
//list = (ArrayList)mySerializer.Deserialize(mem);
}catch(Exception ex)
{
txtState.Text = "反序列化时出现错误:" + ex.ToString() + "\r\n" + txtState.Text;
return null;
}
return list;
}
上面是我序列化和返序列化的方法,但反序列化的时候,提示:
反序列化时出现错误:System.InvalidOperationException: XML 文档(6, 12)中有错误。 ---> System.Xml.XmlException: 意外的 XML 声明。XML 声明必须是文档中的第一个节点并且在它之前不允许出现空白字符。 行 6,位置 12。
在 System.Xml.XmlTextReaderImpl.Throw(Exception e)
在 System.Xml.XmlTextReaderImpl.Throw(String res, String arg)
在 System.Xml.XmlTextReaderImpl.ParsePI(BufferBuilder piInDtdStringBuilder)
在 System.Xml.XmlTextReaderImpl.ParseDocumentContent()
在 System.Xml.XmlTextReaderImpl.Read()
在 System.Xml.XmlTextReader.Read()
在 System.Xml.XmlReader.ReadEndElement()
在 System.Xml.Serialization.XmlSerializationReader.ReadEndElement()
在 Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderEntity.Read2_Entity(Boolean isNullable, Boolean checkType)
在 Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderEntity.Read3_Entity()
--- 内部异常堆栈跟踪的结尾 ---
在 System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
在 System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle)
在 System.Xml.Serialization.XmlSerializer.Deserialize(TextReader textReader)
在 ServerOperate.Server.myXmlDeserialize(Byte[] bytes) 位置 D:\workround\ServerOperate\ServerOperate\Server.cs:行号 366
如何解决?
2 个解决方案
#1
把类的声明也贴出来,
#2
问题已解决...谢谢啦
#1
把类的声明也贴出来,
#2
问题已解决...谢谢啦