使用xml文件存储数据 [英] Using xml file to store data

查看:80
本文介绍了使用xml文件存储数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在WPF编写高尔夫障碍程序,并使用xml文件存储课程和分数数据。

如果我创建另一个课程,然后打开显示信息的窗口,它有没有更新。如果我关闭程序并重新打开,我可以打开显示窗口,它会显示新的课程数据。

有没有一种方法可以在不关闭和重新打开程序的情况下更新?

I'm writing a Golf Handicap program in WPF and using xml files to store the Course and score data.
If I create another course and then open the window that displays the information, it has not updated. If I close the program and reopen, I can open the display window and it shows the new course data.
Is there a way that it will update without closing and reopening the program?

推荐答案

您遇到了数据持久性的问题。您并不需要直接使用XML。在XML中保存数据的最佳方法是数据合同

http://msdn.microsoft.com/en-us/library/ms733127.aspx [ ^ ]。



这种方法最容易使用,而且非侵入性最强也是。您应该将数据放在一些纯数据类或结构中;你可以把你的数据保存在那些类型的许多对象中,制作任何类型的图形,甚至不必是树(这意味着,如果你在关系模型中具有非常典型的循环依赖关系),那很好。您只需告诉数据协定序列化以将对象存储在任何文件或流中,稍后您将能够立即还原所有这些对象。您不需要以任何方式修改您的类型(您不需要继承某些类型,实现某些接口,公开任何内容,什么都不公开),您只需要为您的类型和成员添加几个属性。性能也非常好,因为基于 System.Reflection.Emit ,可以动态生成元数据中的序列化程序集。



请参阅我过去的答案,我提倡这种方法:

创建属性文件...... [ ^ ],

如何在表单应用程序中使用XML文件编写器和阅读器? [ ^ ]。



-SA
You have faced the problem of data persistence. You don't really need to work with XML directly. The best way to persist data in XML is the Data Contract:
http://msdn.microsoft.com/en-us/library/ms733127.aspx[^].

This approach is the easiest to use and most non-intrusive, too. You should have your data in some set of pure-data classes or structures; and you can hold your data in many objects of those types making a graph of any kind, which does not even has to be a tree (it means, if you have circular dependencies quite typical in relational model), it's fine. You simply tell the data contract serialize to store the objects in any file or stream, and later you will be able to restore all these objects at once. You don't need to modify your types in any way (you don't need to inherit certain types, implement certain interface, make anything public, nothing), you only add couple attributes to your types and members. Performance is very good, too, due to automatic generation of serialization assemblies from metadata on the fly, based on System.Reflection.Emit.

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

—SA


这篇关于使用xml文件存储数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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