2014-03-29 37 views
1

我有一個Game application(WP8),我們在那裏保存多個attampts的分數,並顯示給用戶。獨立存儲 - 如何讀取附加數據

我有一個對象與字段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); 

現在3次嘗試與每一個有noOfStonesPicked -12 and noOfFruitsPicked - 20

現在我已經保存此類似:

public static void WritetestTopicState(MyTopic topic) 
     { 
      try 
      { 
       using (var store = IsolatedStorageFile.GetUserStoreForApplication()) 
       { 
        using (StreamWriter sw = new StreamWriter(store.OpenFile("12.xml", FileMode.Append, FileAccess.Write))) 
        { 
         XmlSerializer serializer = new XmlSerializer(typeof(MyTopic)); 
         serializer.Serialize(sw, topic); 
         serializer = null; 
        } 

       } 
      } 
      catch (Exception) 
      { 
       throw; 
      } 
     } 

現在,我怎麼能retrive這些價值和顯示?

編輯

這是我曾嘗試:

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; 
     } 
+0

有什麼不對? – deeiip

回答

1
XmlSerializer serializer = new XmlSerializer(typeof(MyTopic)); 

StreamReader reader = new StreamReader(path); 
_myTopic = (MyTopic)serializer.Deserialize(reader); 
reader.Close(); 

這應該是足夠的反序列化,如果您MyTopic對象是正確的序列化,我的意思是如果性能MyTopic對象正確地歸因於xml序列化。

+0

請參閱我的編輯,我已經嘗試過,第一次當我運行它將存儲和回收3個值,下次我運行時拋出錯誤,意外的XML聲明。 – Goofy

+0

這意味着你的MyTopic對象有一些問題。可能MyTopic對象的屬性沒有正確歸因於xml序列化。 – deeiip