代码之家  ›  专栏  ›  技术社区  ›  Nii Laryea

从WP8中的xml读取时出现System.InvalidOperationException

  •  0
  • Nii Laryea  · 技术社区  · 11 年前

    这是来自的后续问题 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文档(2118)中有错误。”我的代码出了什么问题?

    编辑 :内部异常为“根级别的数据无效。第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,所以这个问题可能很简单,但我可能没有意识到。有什么帮助吗?

    1 回复  |  直到 7 年前
        1
  •  0
  •   Romasz Jaihind    11 年前

    下面的代码是否也给出了一个错误?数据模型的构造是什么?

          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
    { }
    

    上面的代码对我有效。