Pregunta

This is a follow-up question from How to create an empty xml in Windows Phone 8.

I did this to create the xml:

public void create()
    {
        List<DataModel> __dataList = new List<DataModel>();

        XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();
        xmlWriterSettings.Indent = true;

        using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
        {
            using (IsolatedStorageFileStream stream = myIsolatedStorage.OpenFile("Data.xml", FileMode.Create))
            {
                XmlSerializer serializer = new XmlSerializer(typeof(List<DataModel>));
                using (XmlWriter xmlWriter = XmlWriter.Create(stream, xmlWriterSettings))
                {
                    serializer.Serialize(stream, __dataList);
                }
            }
        }
    }

When I try to read it with this code, I get another System.InvalidOperationException

    public void read()
    {
        List<DataModel> __dataList = new List<DataModel>();
        try
        {
            using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (IsolatedStorageFileStream stream = myIsolatedStorage.OpenFile("Data.xml", FileMode.Open))
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(List<DataModel>));
                    __dataList = (List<DataModel>)serializer.Deserialize(stream);
                }
            }
        }
        catch (Exception e)
        {
            string s = e.Message;
            e.ToString();
        }
    }

The exception message is "There is an error in XML document (2, 118)." What is wrong with my code?

Edit: Inner exception is "Data at the root level is invalid. Line 2, position 118."

Edit 2: I read the contents of the xml using StreamReader.ReadToEnd() before deserializing and this is the return string: <?xml version="1.0" encoding="utf-8"?> <ArrayOfDataModel xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" />

This is my first time working with xml, so the issue may be a simple one but I may not realise it. Any help?

¿Fue útil?

Solución

Does the code below also give an error? And what is the construction of DataModel?

      public void create()
  {
     List<DataModel> __dataList = new List<DataModel>();

     //XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();
     //xmlWriterSettings.Indent = true;

     using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
     {
        using (IsolatedStorageFileStream stream = myIsolatedStorage.OpenFile("Data.xml", FileMode.Create))
        {
           try
           {
              XmlSerializer serializer = new XmlSerializer(typeof(List<DataModel>));
              //using (XmlWriter xmlWriter = XmlWriter.Create(stream, xmlWriterSettings))
              //{
              serializer.Serialize(stream, __dataList);
              //}
           }
           catch { }
        }
     }
  }

  public void read()
  {
     List<DataModel> __dataList = new List<DataModel>();
     try
     {
        using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
        {
           using (IsolatedStorageFileStream stream = myIsolatedStorage.OpenFile("Data.xml", FileMode.Open))
           {
              XmlSerializer serializer = new XmlSerializer(typeof(List<DataModel>));
              __dataList = (List<DataModel>)serializer.Deserialize(stream);
           }
        }
     }
     catch (Exception e)
     {
        string s = e.Message;
        e.ToString();
     }
  }

And somewhere:

public class DataModel
{ }

That code above works for me.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top