C# 独立存储-如何读取附加数据
我有一个C# 独立存储-如何读取附加数据,c#,wpf,silverlight,windows-phone-8,isolatedstorage,C#,Wpf,Silverlight,Windows Phone 8,Isolatedstorage,我有一个游戏应用程序(WP8),在那里我们保存了多个攻击的分数并将其显示给用户 我有一个对象,其字段为noOfStonesPicked和noOfFruitsPicked 这是我的密码: MyTopic topicObj = new MyTopic (); for (int i = 0; i <= 2; i++) { Test mt = new Test(); mt.noOfStonesPicked =
游戏应用程序(WP8)
,在那里我们保存了多个攻击的分数并将其显示给用户
我有一个对象,其字段为noOfStonesPicked和noOfFruitsPicked
这是我的密码:
MyTopic topicObj = new MyTopic ();
for (int i = 0; i <= 2; i++)
{
Test mt = new Test();
mt.noOfStonesPicked = 12;
mt.noOfFruitsPicked= 20;
topicObj.Stats.Add(mt);
}
WritetestTopicState(topicObj);
现在如何检索这些值并显示
编辑
这就是我尝试过的:
public static MyTopic ReadMockTestTopicState()
{
MyTopic topic = null;
try
{
using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication())
{
// Read application settings.
if (isoStore.FileExists("11.xml"))
{
using (var store = IsolatedStorageFile.GetUserStoreForApplication())
{
using (StreamReader SR = new StreamReader(store.OpenFile("12.xml", FileMode.Open, FileAccess.Read)))
{
XmlSerializer serializer = new XmlSerializer(typeof(MyTopic));
topic = (MyTopic)serializer.Deserialize(SR);
serializer = null;
}
}
}
else
{
// If setting does not exists return default setting.
topic = new MyTopic();
}
}
}
catch (Exception)
{
throw;
}
return topic;
}
这对于反序列化来说应该足够了,如果您的
MyTopic
对象可以正确序列化,我的意思是如果MyTopic对象的属性可以正确地用于xml序列化。请参阅我的编辑,我已经尝试过了,第一次运行时,它将存储并检索3个值,下次运行时,它会抛出一个错误,意外的XML声明
。这意味着您的MyTopic对象中存在一些问题。对于xml序列化,MyTopic对象的属性可能没有正确的属性。
public static MyTopic ReadMockTestTopicState()
{
MyTopic topic = null;
try
{
using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication())
{
// Read application settings.
if (isoStore.FileExists("11.xml"))
{
using (var store = IsolatedStorageFile.GetUserStoreForApplication())
{
using (StreamReader SR = new StreamReader(store.OpenFile("12.xml", FileMode.Open, FileAccess.Read)))
{
XmlSerializer serializer = new XmlSerializer(typeof(MyTopic));
topic = (MyTopic)serializer.Deserialize(SR);
serializer = null;
}
}
}
else
{
// If setting does not exists return default setting.
topic = new MyTopic();
}
}
}
catch (Exception)
{
throw;
}
return topic;
}
XmlSerializer serializer = new XmlSerializer(typeof(MyTopic));
StreamReader reader = new StreamReader(path);
_myTopic = (MyTopic)serializer.Deserialize(reader);
reader.Close();