2012-07-16 63 views
0

我知道如何從XML文件中讀取內容,但我不知道如何寫入內容。如何將整數變量保存到XML文件?

例如,我從XML文件中讀取整數變量並對其進行修改。修改後,我想將整型變量保存到同一個XML文件中。舊的整數值必須用新值替換。我的遊戲在Windows 7

運行我加載XML文件與此代碼:

protected override void LoadContent() 
{ 
    scorexml = Content.Load<List<Gamescore>>("Score"); 
    foreach (Gamescore score in scorexml) 
    { 
     score.Load(Content); 
    } 
} 


public class Gamescore 
{ 
    int score; 
    public int Score 
    { 
     get { return score; } 
     set { score = value; } 
    } 
} 
+0

什麼是「內容」? – 2012-07-16 13:29:16

+0

檢查這 http://stackoverflow.com/questions/367730/how-to-change-xml-value-file-using-c-sharp 如果它適合你,我會張貼它作爲答案! – 2012-07-16 13:29:33

+0

@ DanielA.White這是XNA加載遊戲資產的類。 – Tharwen 2012-07-16 13:30:43

回答

0

我可以建議你兩種方法。

  1. 使用代表您的XML類,並實現了ISerializable interface
    • 反序列化XML到對象的類的實例。
    • 處理數據(設置整數值或其他)。
    • 將對象序列化到xml文件中。
  2. 使用Linq to XML來處理XML:
    • 創建XML文件的XDocument。
    • 用Linq處理XDocument。
    • 將XDocument保存到xml文件中。
+0

我試過這個教程:http://wellroundedgeek.com/post/2011/01/25/Simple-XNA-Cross-Platform-Settings-Manager.aspx – 2012-07-20 21:35:43

相關問題