C# 将数据保存在XML或数据库中更好吗?

C# 将数据保存在XML或数据库中更好吗?,c#,wpf,simulation,.net-4.5,C#,Wpf,Simulation,.net 4.5,我们开发了一个航空公司模拟游戏,我们当前的结构将用户数据保存在XML文件中(以及所有游戏数据,如机场统计数据、飞机信息等) 就性能和功能而言,在本地计算机上存储此数据的最佳方式是什么?我听到了双方的一些观点,但没有真正具体或有实例支持的答案。虽然我们的原始数据XMLS比较小(如果您将数据保存在XML文件中,那么考虑它的Oracle BokLeDB XML/BASEX/SEDNA的XML数据库),可以使用DB API进行数据操作,也可以使用XQuery查询XML。 此外,如果您的用户数据、机场统计

我们开发了一个航空公司模拟游戏,我们当前的结构将用户数据保存在XML文件中(以及所有游戏数据,如机场统计数据、飞机信息等)


就性能和功能而言,在本地计算机上存储此数据的最佳方式是什么?我听到了双方的一些观点,但没有真正具体或有实例支持的答案。虽然我们的原始数据XMLS比较小(

如果您将数据保存在XML文件中,那么考虑它的Oracle BokLeDB XML/BASEX/SEDNA的XML数据库),可以使用DB API进行数据操作,也可以使用XQuery查询XML。 此外,如果您的用户数据、机场统计数据等信息可能因行而异,则应该使用XML数据库

如果您的数据模型结构良好,则可以使用MySql或PostGreSql等开源数据库来处理所有数据

这里,对于数据添加/更新的预期数据库大小,两者都会很好。您应该考虑数据模型来选择数据仓库类型。

如果选择XML数据库,则可以重用数据访问/保存代码

如果选择RDBMS,则应为应用程序层编写代码


希望,这些见解和进一步的研究将对您有所帮助。

如果您不需要手动编辑文件,您可以尝试使用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;
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
命名空间WpfApplication2
{
[可序列化()]
公营儿童
{
公共字符串属性1{get;set;}
}
[可序列化()]
公共类TestClass
{
公共int属性1{get;set;}
公共字符串属性2{get;set;}
公共日期时间属性3{get;set;}
公共子项{get;set;}
}
/// 
///MainWindow.xaml的交互逻辑
/// 
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
TestClass TestClass=新的TestClass()
{
属性1=1,
Property2=“测试”,
Property3=日期时间。现在,
Child=新的Child()
{
Property1=“测试”,
}
};
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter=new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
System.IO.MemoryStream MemoryStream=新的System.IO.MemoryStream();
序列化(memoryStream,testClass);
memoryStream.Position=0;
TestClass deserialized=格式化程序。反序列化(memoryStream)为TestClass;
}
}
}

您多长时间加载/保存一次数据?在游戏过程中它有多重要?如果只是在启动和特定保存期间,这可能并不重要。您是否需要实际查询数据,或者您真的只是保存和加载整个数据?专门用于加载和保存游戏。我们似乎被当前计算游戏信息时会出现性能问题,因此为了减少保存时间,我们可以尝试执行一种增量自动保存,在创建保存文件时,将保持静态的信息添加到保存文件中。是否玩过游戏,然后浏览到游戏文件夹,查看您所有的个人保存数据等?最专业的游戏c公司似乎采取了“保存到文件”的方法。你看过其他类似的游戏吗?只是想一想。我看过其他的游戏(虽然最近不是很特别)。我见过不少游戏使用专有格式保存游戏,但我不确定性能优势是否足以证明我们的情况。我最喜欢这个。您可以将它与GZipStream结合使用来压缩输出,我打赌这会使其在大型游戏保存时变得更小。显然,我们在过去尝试过这个方法,但在序列化包含其他对象的对象时遇到问题。是否有智慧之词/指向引用该对象的内容的链接?快速谷歌搜索没有显示任何非常有用的内容。应该可以,我已更新示例以显示包含另一个对象的对象。