从 WP8 中的 xml 读取时出现 System.InvalidOperationException [英] System.InvalidOperationException when reading from xml in WP8

查看:19
本文介绍了从 WP8 中的 xml 读取时出现 System.InvalidOperationException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是来自 How在 Windows Phone 8 中创建一个空的 xml.

我这样做是为了创建 xml:

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

当我尝试用这段代码读取它时,我得到另一个 System.InvalidOperationException

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

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

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

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

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

Edit 2:在反序列化之前,我使用 StreamReader.ReadToEnd() 读取了 xml 的内容,这是返回字符串:<ArrayOfDataModel xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"/>

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" />

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

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

推荐答案

下面的代码是否也报错?DataModel 的构造是什么?

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

某处:

public class DataModel
{ }

上面的代码对我有用.

这篇关于从 WP8 中的 xml 读取时出现 System.InvalidOperationException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆