Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 读取WP8中的xml时System.InvalidOperationException_C#_Xml_Windows Phone 8_Xml Serialization_Xml Deserialization - Fatal编程技术网

C# 读取WP8中的xml时System.InvalidOperationException

C# 读取WP8中的xml时System.InvalidOperationException,c#,xml,windows-phone-8,xml-serialization,xml-deserialization,C#,Xml,Windows Phone 8,Xml Serialization,Xml Deserialization,这是我的后续问题 我这样做是为了创建xml: public void create() { List<DataModel> __dataList = new List<DataModel>(); XmlWriterSettings xmlWriterSettings = new XmlWriterSettings(); xmlWriterSettings.Indent = true; using

这是我的后续问题

我这样做是为了创建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);
                }
            }
        }
    }
异常消息是“XML文档(2118)中有错误。”我的代码有什么问题

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

编辑2:在反序列化之前,我使用
StreamReader.ReadToEnd()
读取xml的内容,这是返回字符串:


这是我第一次使用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))
        {
           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();
     }
  }

上面的代码对我有用

您应该尝试显示XML文件的内容,以了解发生了什么,特别是,a:在(2118)的XML文件中有什么,b:什么是
.InnerException.Message
.InnerException.InnerException.Message
.InnerException.InnerException.Message
,等等
XmlSerializer
实际上给出了非常详细的消息:您只需进一步查看
异常
@KooKiz xml文件将存储在哪里?@NiiLaryea使用您的流创建一个StreamReader,然后显示
ReadToEnd
方法的结果。我的问题不在于
Create()
method,它与
read()方法有关。我认为问题在于XmlWriterSettings XmlWriterSettings=new XmlWriterSettings();xmlWriterSettings.Indent=true;不使用xmlWriter就可以做到这一点——正如我上面所描述的。我刚试过,效果不错。即使使用OpenFile(),我也去掉了
XmlWriterSettings
行,异常现在显示为:“XML文档(0,0)中有错误。”内部异常显示为:“根元素丢失”。确定-您的数据模型类是公共类吗?公共类数据模型{}并且您不再使用(XmlWriter XmlWriter=XmlWriter.Create(stream,xmlWriterSettings))?我的数据模型类不是公共的。我不敢相信这一直是个问题。谢谢你,伙计
      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
{ }