[英]'System.InvalidOperationException' when LINQ to SQL using WCF on WP8
[英]System.InvalidOperationException when reading from xml in WP8
這是如何在Windows Phone 8中創建空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))
{
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,因此問題可能很簡單,但我可能沒有意識到。 有什么幫助嗎?
下面的代碼還會給出錯誤嗎? 數據模型的構造是什么?
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
{ }
上面的代碼對我有用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.