将对象列表保存到XML文件并使用C#加载该列表的最佳方法是什么?

Sir*_*use 4 .net c#

我的应用程序中有一个"Gesture"类列表:

List<Gesture> gestures = new List<Gesture>();
Run Code Online (Sandbox Code Playgroud)

这些手势类非常简单:

public class Gesture
{
    public String Name { get; set; }
    public List<Point> Points { get; set; }
    public List<Point> TransformedPoints { get; set; }    

    public Gesture(List<Point> Points, String Name)
    {
        this.Points = new List<Point>(Points);
        this.Name = Name;
    }
}
Run Code Online (Sandbox Code Playgroud)

我想允许用户将"手势"的当前状态保存到文件中,并且还能够加载包含手势数据的文件.

在C#中执行此操作的标准方法是什么?

我应该使用序列化吗?我应该自己编写一个类来处理这个XML文件的编写/读取吗?还有其他方法吗?

Gis*_*shu 6

看一下.net中的XmlSerializer类型和XmlSerialization

这里......找到了一个可以满足您需求的例子 http://www.switchonthecode.com/tutorials/csharp-tutorial-xml-serialization

  • 需要注意的一点是,他需要公开一个公共无参数构造函数,以便在此类型上使用XmlSerializer. (2认同)