我正在为 wp7 开发应用程序。我正在使用 datacontractserializer 将应用程序的状态存储在隔离的存储文件中。现在,当我尝试序列化对象时,有时我会遇到此异常。
处于 EndRootElement 状态的令牌 EndElement 将导致无效的 XML 文档。如果要编写 XML 片段,请确保将 ConformanceLevel 设置设置为 ConformanceLevel.Fragment 或 ConformanceLevel.Auto。
这是我正在序列化的课程
[DataContract]
public class TcpSessionProperties
{
[DataMember]
public int NumberOfReceivedStanzas { get { return _numberOfReceivedStanzas; } set { _numberOfReceivedStanzas = value; } }
[DataMember]
public int NumberOfPacketsSent { get { return _numberOfPacketsSent; } set { _numberOfPacketsSent = value; } }
[DataMember]
public List<TcpPacket> PendingPacketsToSendQueue
{
get { return _pendinPacketsToSendQueue; }
set
{
LogUtility.Log(Thread.CurrentThread.GetHashCode());
_pendinPacketsToSendQueue = value;
}
}
}
cla
这是我正在序列化和反序列化的代码
public class AppCacheIsolatedFile
{
public static void Add(object val)
{
private static object _objectToSave;
lock (_storageLock)
{
/*
if (PhoneApplicationService.Current.State.ContainsKey(key))
PhoneApplicationService.Current.State[key] = val;
else
PhoneApplicationService.Current.State.Add(key, val);
*/
_objectToSave = val;
WriteObject(NameOfFile, val, typeof(StreamingSocketConnection.StreamingSocketConnection));
}
public static void WriteObject(string fileName,object val,Type type)
{
try
{
Console.WriteLine(
"Creating a Person object and serializing it.");
using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(fileName, FileMode.Create, IsoStore))
{
DataContractSerializer dcs = new DataContractSerializer(type);
dcs.WriteObject(stream, val);
}
}
catch (Exception exception)
{
LogUtility.NotifyUserAndLog("Exception occured in isolatd storage file while writing "+exception.StackTrace);
}
}
}
该异常发生在 dcs.writeobject。我不知道为什么会这样。