基本上有 DrinkCategories 持有一个可观察的 DrinkCategory 集合。这个嵌套的 ObservableCollection 用于为 WP8 中的 LongListSelector 提供数据。
this.DrinkCategories = new ObservableCollection<DrinkCategory>();
DrinkCategory 本身实现了 ObservableCollection 并添加了一个“名称”属性。
public class DrinkCategory : System.Collections.ObjectModel.ObservableCollection<DrinkViewModel>
{
public string Name { get; set; }
public void AddDrink(DrinkViewModel drinkItem)
{
this.Items.Add(drinkItem);
}
}
我的 XMLserializer 将“项目”存储在 DrinkCategory 中,但不存储“名称”。
using (IsolatedStorageFileStream stream = myIsolatedStorage.OpenFile("AvailableDrinks.xml", FileMode.Create))
{
XmlSerializer serializer = new XmlSerializer(typeof(ObservableCollection<DrinkCategory>));
using (XmlWriter xmlWriter = XmlWriter.Create(stream, xmlWriterSettings))
{
serializer.Serialize(xmlWriter, DrinkCategories);
}
}
这是输出:
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfArrayOfDrinkViewModel xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<ArrayOfDrinkViewModel>
<DrinkViewModel>
Properties of DVM
</DrinkViewModel>
<DrinkViewModel>
Properties of DVM
</DrinkViewModel>
<DrinkViewModel>
Properties of DVM
</DrinkViewModel>
</ArrayOfDrinkViewModel>
<ArrayOfDrinkViewModel>
<DrinkViewModel>
Properties of DVM
</DrinkViewModel>
<DrinkViewModel>
Properties of DVM
</DrinkViewModel>
<DrinkViewModel>
Properties of DVM
</DrinkViewModel>
</ArrayOfDrinkViewModel>
</ArrayOfArrayOfDrinkViewModel>
我的错误在哪里?
编辑:问题似乎来自“DrinkCategory”类的实现,因为“DrinkCategory”的单个序列化对象包含它的饮料,但不是它的名称。XmlSerializer serializer = new XmlSerializer(typeof(DrinkCategory));