Token EndElement in state EndRootElement would result in an invalid XML document

2.3k views Asked by At

I am developing an application for wp7.i am storing the state of my application in isolatedstoragefile using datacontractserializer.Now when i try to serialize the object sometimes i face this exception.

Token EndElement in state EndRootElement would result in an invalid XML document. Make sure that the ConformanceLevel setting is set to ConformanceLevel.Fragment or ConformanceLevel.Auto if you want to write an XML fragment.

here is my class which i am serializing

[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

Here is my code where i am serializing and deserializing

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); 
        }


    }


    }

That exception occurs at dcs.writeobject. I donnot why is this happening.

0

There are 0 answers