![](/img/trans.png)
[英]What's the easiest way to save an object to a file without serialization attributes?
[英]What is the easiest way to save an Observable collectin of object to an XML file?
我有一個包含客戶對象的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; }
}
將其轉儲到XML文件中以便日后閱讀的最簡單方法是什么?
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);
}
要從文件中重新加載數據:
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.