【问题标题】:System.InvalidOperationException when reading from xml in WP8从 WP8 中的 xml 读取时出现 System.InvalidOperationException
【发布时间】:2013-10-28 12:07:02
【问题描述】:

这是来自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:我在反序列化之前使用 StreamReader.ReadToEnd() 读取了 xml 的内容,这是返回字符串: <?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,所以问题可能很简单,但我可能没有意识到。有什么帮助吗?

【问题讨论】:

  • 您应该尝试显示您的 XML 文件的内容以了解发生了什么
  • 特别是,a:(2,118) 处的 xml 文件中有什么,b:.InnerException.Message.InnerException.InnerException.Message.InnerException.InnerException.InnerException.Message 等是什么? XmlSerializer 实际上给出了真的很 详细的信息:您只需要进一步查看Exception 的几个级别
  • @KooKiz xml 文件将存储在哪里?
  • @NiiLaryea 使用您的流创建一个 StreamReader,然后显示 ReadToEnd 方法的结果。

标签: c# xml windows-phone-8 xml-serialization xml-deserialization


【解决方案1】:

下面的代码是否也会出错?以及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
{ }

上面的代码对我有用。

【讨论】:

  • 我的问题不在于create() 方法,而在于read() 方法。
  • 我认为问题出在 XmlWriterSettings xmlWriterSettings = new XmlWriterSettings(); xmlWriterSettings.Indent = true;在没有 xmlWriter 的情况下执行此操作 - 就像我上面描述的那样。我刚刚尝试过,它有效。即使使用 OpenFile()
  • 我取出了XmlWriterSettings 行,现在异常显示为:“XML 文档中存在错误 (0, 0)。”内部异常显示:“缺少根元素。”
  • OK - 您的 DataModel 类是公共类吗? public class DataModel { } 你摆脱了使用 (XmlWriter xmlWriter = XmlWriter.Create(stream, xmlWriterSettings)) ?
  • 我的 DataModel 类不是公开的。我不敢相信这一直是问题所在。非常感谢人
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-11
  • 2015-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-21
相关资源
最近更新 更多