[英]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.