将类存储到XML文件中 [英] store the class into XML file

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

问题描述

我有一个类,由诸如ID,Name,Address的属性组成.我有一个类别列表,我想保存xml文件,该怎么办?

I have a class consisting of attributes such as ID , Name , Address . I have a list of classes , I want to save the xml file , how can I do ? thsnks so much

推荐答案

使用Data Contract.这是最非侵入性且易于使用的方法.您只需将属性添加到类型和成员,因此不需要对类型本身进行修改.

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

另请参阅我过去提倡使用此方法的答案:
如何在我的表单应用程序? [ ^ ],
创建属性文件... [反序列化json字符串数组 [
You can store (persist, store/load) not just a class, but an arbitrary object graph of any structure (not necessarily hierarchical) using Data Contract. This is most non-intrusive and easy-to-use approach; you only add attributes to types and members, so not modifications of types themselves are required.

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

See also 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


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

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