如何在不使用数据库的情况下将数据保存在C#中? [英] How to save data in c# without using database?

查看:278
本文介绍了如何在不使用数据库的情况下将数据保存在C#中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作纸牌游戏.我需要在不使用数据库的情况下保存前10名玩家
因为我不需要仅用于最高得分列表的数据库.现在,我该如何保存数据?

i want to build a card game.i need to save top 10 players without using database
because i don''t need database just only for top score list. Now, how can i save data ??

推荐答案

如果将所有数据放入序列化的对象中,则可以保存恢复对象.(
If you put all your data into serialized objects, then can save an restore the object.(Load and save objects to XML using serialization[^]). I have found a nice generic for doing the saving and restoring:
/// <summary>
/// Serializes an object.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="serializableObject"></param>
/// <param name="fileName"></param>
public void SerializeObject<T>(T serializableObject, string fileName)
{
    if (serializableObject == null) { return; }

    try
    {
        XmlDocument xmlDocument = new XmlDocument();
        XmlSerializer serializer = new XmlSerializer(serializableObject.GetType());
        using (MemoryStream stream = new MemoryStream())
        {
            serializer.Serialize(stream, serializableObject);
            stream.Position = 0;
            xmlDocument.Load(stream);
            xmlDocument.Save(fileName);
            stream.Close();
        }
    }
    catch (Exception ex)
    {
        //Log exception here
    }
}


/// <summary>
/// Deserializes an xml file into an object list
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="fileName"></param>
/// <returns></returns>
public T DeSerializeObject<T>(string fileName)
{
    if (string.IsNullOrEmpty(fileName)) { return default(T); }

    T objectOut = default(T);

    try
    {
        string attributeXml = string.Empty;

        XmlDocument xmlDocument = new XmlDocument();
        xmlDocument.Load(fileName);
        string xmlString = xmlDocument.OuterXml;

        using (StringReader read = new StringReader(xmlString))
        {
            Type outType = typeof(T);

            XmlSerializer serializer = new XmlSerializer(outType);
            using (XmlReader reader = new XmlTextReader(read))
            {
                objectOut = (T)serializer.Deserialize(reader);
                reader.Close();
            }

            read.Close();
        }
    }
    catch (Exception ex)
    {
        //Log exception here
    }

    return objectOut;
}


您可以将数据保存在文件中:
http://msdn.microsoft.com/en-us/library/8bh11f1k.aspx [ ^ ]
You can save data in a file:
http://msdn.microsoft.com/en-us/library/8bh11f1k.aspx[^]


保存数据的方法有很多,但是您应该首选一些通用且灵活的方法.这就是为什么我建议您使用序列化的原因,并且媒体将是一些XML或JSON文件.

我建议采用最先进,最"OOP"的序列化方法,这种方法最健壮,同时最容易应用.这是关于使用数据合同:
http://msdn.microsoft.com/en-us/library/ms733127.aspx [ ^ ].

这种方法也是最不介入的.您所需要的只是通过编写一个或多个纯数据类或结构来定义某种数据模型.要使其成为合同,只需将[DataMember][DataContract]属性添加到成员和类型.当您使用数据协定序列化程序时,它会将整个任意数据图(因此,它可能只是一个对象,但可以是对象的整个图,它们以任何顺序相互引用)存储到文件或流中,并且将完全按照存储时的过程内存中的方式还原它.

请在我倡导这种方法的地方查看我过去的答案:
如何在我的表单应用程序? [ ^ ],
创建属性文件... [反序列化json字符串数组 [
There are many ways to save data, but you should prefer some universal and flexible way. That''s why I suggest you use serialization, and the media would be some XML or JSON file.

I would advice the most advanced and "most OOP" approach to serialization, which is most robust by at the same time is the easiest to apply. This is about using Data Contracts:
http://msdn.microsoft.com/en-us/library/ms733127.aspx[^].

This approach is also the most non-intrusive. All you need is to define some data model by writing one or more pure-data classes or structures. To make it a contract, you just add [DataMember] and [DataContract] attributes to members and types. When you use a data contract serializer, it will store the whole arbitrary data graph (so, it may be just one object, but could be a whole graph of objects referencing each other in any order) to a file or a stream, and will restore it back exactly in the way it was in your process memory when you stored it.

Please see my past answers where I advocate this approach:
How can I utilize XML File streamwriter and reader in my form application?[^],
Creating property files...[^],
deseralize a json string array[^].

—SA


这篇关于如何在不使用数据库的情况下将数据保存在C#中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆