2013-03-16 70 views
1

我們開發了航空模擬遊戲,我們目前的結構將用戶數據保存在XML文件中(以及所有遊戲數據,如機場統計數據,飛機信息等)。將數據保存在XML或數據庫中會更好嗎?

性能和功能方面,在本地計算機上存儲此數據的最佳方式是什麼?我聽到了雙方的一些,但沒有真正的具體或實例支持的答案。雖然我們的原始數據XML較小(< 150KB),但保存的遊戲非常大(3-10MB),並且跨越數千行或多或少的無組織數據。

想法,建議或建議?

+1

您多長時間一次加載/保存數據?在遊戲過程中它有多重要?如果只是在啓動和特定保存期間,它可能並不重要。 – Oded 2013-03-16 14:41:19

+0

你是否真的需要實際*查詢*,或者你真的只是保存和加載整個事情? – 2013-03-16 14:41:39

+0

專用將用於加載和保存遊戲。我們似乎在計算遊戲信息時遇到了性能問題,因此爲了減少保存時間,我們可能會嘗試執行一種增量自動保存,在保存文件創建時添加對保存文件保持靜態的信息。 – mikedugan 2013-03-16 14:44:12

回答

3

如果你不需要手工編輯文件,你可以嘗試使用BinaryFormatter來序列化&反序列化你的數據,應該比XmlSerializer快很多。

這是一個如何使用它的例子。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Navigation; 
using System.Windows.Shapes; 

namespace WpfApplication2 
{ 
    [Serializable()] 
    public class Child 
    { 
     public string Property1 { get; set; } 
    } 

    [Serializable()] 
    public class TestClass 
    { 

     public int Property1 { get; set; } 
     public string Property2 { get; set; } 
     public DateTime Property3 { get; set; } 
     public Child Child { get; set; } 
    } 


    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 

      TestClass testClass = new TestClass() 
      { 
       Property1 = 1, 
       Property2 = "test", 
       Property3 = DateTime.Now, 
       Child = new Child() 
       { 
        Property1 = "test", 
       } 
      }; 

      System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); 
      System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(); 
      formatter.Serialize(memoryStream, testClass); 

      memoryStream.Position = 0; 
      TestClass deserialized = formatter.Deserialize(memoryStream) as TestClass; 
     } 
    } 
} 
+0

我最喜歡這個。你可以將它與GZipStream結合起來壓縮輸出,我敢打賭它會使它在保存大型遊戲時變得非常小。 – 2013-03-16 15:04:19

+0

顯然我們已經在過去試過這個,但是在序列化包含其他對象的對象時遇到了一個問題。任何智慧的話/鏈接到內容引用的?快速谷歌搜索沒有顯示任何令人難以置信的有用 – mikedugan 2013-03-16 15:43:37

+0

應該正常工作,我已更新示例以顯示包含另一個對象的對象。 – Andy 2013-03-16 16:02:51

0

如果您將數據保存爲XML文件,那麼請考慮一個XML數據庫,就像Oracle BerkleyDB XML/BaseX/Sedna一樣。您可以使用DB API進行數據操作,也可以使用XQuery來查詢該XML。 Berkley DB XML數據庫性能良好。

如果您的用戶數據,機場統計數據等信息可能因行而異,您還應該使用XML數據庫。

如果您的數據模型結構良好,那麼您可以使用像MySql或PostGreSql這樣的開源數據庫來處理所有數據。

在這裏,兩者都可以很好地處理數據添加/更新所需的數據庫大小。您應該考慮選擇數據存儲庫類型的數據模型。

如果您選擇XML數據庫,您的數據訪問/保存代碼可能會被重用。

如果您選擇RDBMS,則應該爲您的應用程序層編碼。

希望,這種洞察力和進一步的研究將幫助你。

相關問題