简体   繁体   English

将对象的可观察对象集合保存到XML文件的最简单方法是什么?

[英]What is the easiest way to save an Observable collectin of object to an XML file?

I've have an Observable collection containing customer objects: 我有一个包含客户对象的Observable集合:

  public class Customer
  {
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Street { get; set; }
    public string Location { get; set; }
    public string ZipCode { get; set; }
  }

What is the easiest way to dump this out to an XML file so I can read it in later? 将其转储到XML文件中以便日后阅读的最简单方法是什么?

XML serialization : XML序列化:

ObservableCollection<Customer> customers = new ObservableCollection<Customer>();
...

XmlSerializer xs = new XmlSerializer(typeof(ObservableCollection<Customer>));
using (StreamWriter wr = new StreamWriter("customers.xml"))
{
    xs.Serialize(wr, customers);
}

To reload the data from the file : 要从文件中重新加载数据:

XmlSerializer xs = new XmlSerializer(typeof(ObservableCollection<Customer>));
using (StreamReader rd = new StreamReader("customers.xml"))
{
    customers = xs.Deserialize(rd) as ObservableCollection<Customer>;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将对象保存到没有序列化属性的文件的最简单方法是什么? - What's the easiest way to save an object to a file without serialization attributes? 从外部Xml到C#对象,最简单的方法是什么? - External Xml to C# Object, what is the easiest way? 将此XML文档转换为我的对象的最简单方法是什么? - What is the easiest way to convert this XML document to my object? 解析具有许多不同级别的XML文件的最简单方法是什么? - What's the easiest way to Parse an XML file with many different levels? 什么是序列化和反序列化为XML的最简单方法 - What is the easiest way to serialize and deserialize to XML 将密码保存到注册表时,加密密码的最简单方法是什么? - What is the easiest way to encrypt a password when I save it to the registry? 使用 C# 处理 xml 文件的最简单方法是什么? - What is the easiest way of handling xml files with C#? 通过代码将文件保存到备份服务器的最简单和/或最安全的方式? - easiest and/or safest way(s) to save a file to a backup server by code and how? 将数据从文件保存到对象C#的正确方法是什么 - What is proper way to save data from file to object C# 从xml文件中删除无效字符的最简单方法? - easiest way to remove invalid characters from a xml file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM