0

这是How to create an empty xml in Windows Phone 8 中的后续问题。

我这样做是为了创建 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);
                }
            }
        }
    }

当我尝试用这段代码阅读它时,我得到了另一个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();
        }
    }

异常消息是“XML 文档中存在错误 (2, 118)”。我的代码有什么问题?

编辑:内部异常是“根级别的数据无效。第 2 行,位置 118。”

编辑 2:我在反序列化之前读取了 xml 的内容,StreamReader.ReadToEnd()这是返回字符串: <?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" />

这是我第一次使用 xml,所以问题可能很简单,但我可能没有意识到。有什么帮助吗?

4

1 回答 1

0

下面的代码是否也会出错?而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();
     }
  }

在某处:

public class DataModel
{ }

上面的代码对我有用。

于 2013-10-28T13:08:35.970 回答