将对象保存到XML文件c# [英] Saving objects to XML file c#

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

问题描述

我想保存对象(以及加载已保存的对象),从列表,字符串,复选框状态,单选按钮状态等到XML文件,但我对XML完全不熟悉并想知道如何实现这一目标?

解决方案

在此处扩展解决方案#3:我想强烈建议:



1.你使用WCF(DataContract,DataMember)用于序列化和反序列化对象。它功能齐全,易于使用且功能强大。



它生成的XML,虽然详细,但可以压缩...根据我的经验...至少减半(通常低于25%) XML的大小。



2.你意识到你需要创建类来进行serailze /反序列化。原因之一是:



2.a.你不能序列化WinForm控件,比如CheckBox(长篇故事)。



所以,如果我的表单上有五个CheckBox,那么Class序列化可能看起来像这样:

  //   required  
使用 System.Runtime.Serialization;
使用 System.Windows.Forms
使用 System.Collections.Generic;

[DataContract] // 使用WCF
public class SerializeForm1
{
public SerializeForm1()
{
NmToChkState = new Dictionary< string,CheckState>();
};

private Dictionary< string,CheckState> _nmToChkState;
[DataMember]
public Dictionary< string,CheckState> NmToChkState
{
set {_ nmToChkState = value ; }
get { return _nmToChkState; }
}
}

当我准备好保存Form的状态时,我做了正确的事情来插入CheckBox(Key)名称的KeyValuePairs,以及CheckState(Value)。



当我对这个写入XML对象进行反序列化时,我会遍历Dictionary并做正确的事情来恢复CheckState值每个CheckBox都基于他们的名字。



这里有很多好文章和教程;从谢尔盖在解决方案#3中推荐的那些开始。


直接使用JSON或 XmlWriter ,如解决方案1和3中所建议的那样,导致低级别,特别是ad-hoc代码不足。太多可以接受了。您应该更好地使用不可知的类型和特定于您的数据的任何内容,并自动为您完成所有操作。这称为序列化 https://en.wikipedia.org/wiki/Serialization [ ^ ]。



在.NET中最好的序列化,同时最强大和最容易使用的是 Data Contract https://msdn.microsoft.com/en-us/library/ms733127%28v=vs.110%29.aspx [ ^ ]。



请查看我过去的答案:

如何将数据合同从C#/ WCF代码传递给托管C ++ [ ^ ],

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

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

解析json字符串数组 [ ^ ],

反序列化列表< class object>导致objects属性失去对基类对象的引用 [ ^ ]。



-SA


您使用的是WPF吗?您可以使用XamlWriter编写XAML文件。



https://msdn.microsoft.com/en-us/library/system.windows.markup.xamlwriter(v = vs.110)的.aspx [ ^ ]

I want to save objects(and also load saved objects) ranging from lists, strings, checkbox statuses, radiobutton statuses etc to an XML file but I'm totally new to XML and wondering how this can be achieved?

解决方案

To expand on Solution #3 here: I'd like to strongly suggest:

1. you use WCF (DataContract, DataMember) to serialize and de-serialize your objects. It is full-featured, easy to use, and powerful.

The XML it produces, while "verbose," can be compressed ... in my experience ... down to at least half (often less than 25%) the size of the XML.

2. you realize that you will need to create Classes to serailze/de-serialize. One reason why is:

2.a. you cannot serialize WinForm Controls, like the CheckBox (long story there).

So, if I have five CheckBoxes on my Form, the Class will serialize might look like this:

 // required
using System.Runtime.Serialization;
using System.Windows.Forms
using System.Collections.Generic;

[DataContract]  // using WCF
public class SerializeForm1
{
    public SerializeForm1()
    {
        NmToChkState = new Dictionary<string, CheckState>();
    };

    private Dictionary<string, CheckState> _nmToChkState;
    [DataMember]
    public Dictionary<string, CheckState> NmToChkState
    {
        set { _nmToChkState = value; }
        get { return _nmToChkState; }
    }
}

When I am ready to save the state of the Form, I do the right thing to insert KeyValuePairs of the name of the CheckBox (Key), and the CheckState (Value).

When I de-serialize this written-to-XML object, I then iterate over the Dictionary and do the right thing to restore the CheckState values of each CheckBox based on their Names.

There are lots of good articles, and tutorials here; start with the ones Sergey recommended in Solution #3.


Using JSON or XmlWriter directly, as advised in Solutions 1 and 3, will lead to inadequately low-level, and, most likely, ad-hoc code. Too much to be acceptable. You should better use something which is agnostic to types and anything specific to your data and does everything automatically for you. This is called serialization: https://en.wikipedia.org/wiki/Serialization[^].

And best kind of serialization in .NET, the most robust and the easiest to use at the same time is Data Contract: https://msdn.microsoft.com/en-us/library/ms733127%28v=vs.110%29.aspx[^].

Please see my past answers:
how to pass a datacontract from a C#/WCF code to Managed C++[^],
How can I utilize XML File streamwriter and reader in my form application?[^],
Creating property files...[^],
deseralize a json string array[^],
deserializing list<class object> cause the objects property lose their reference to the objects of the base class[^].

—SA


Are you using WPF ? The you can use XamlWriter to write a XAML file.

https://msdn.microsoft.com/en-us/library/system.windows.markup.xamlwriter(v=vs.110).aspx[^]


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

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