基本上有DrinkCategories持有DrinkCategory的observableCollection。这个嵌套的ObservableCollection用于为WP8中的LongListSelector提供数据。
this.DrinkCategories = new ObservableCollection<DrinkCategory>();
DrinkCategory本身实现ObservableCollection并添加“Name”属性。
public class DrinkCategory : System.Collections.ObjectModel.ObservableCollection<DrinkViewModel>
{
public string Name { get; set; }
public void AddDrink(DrinkViewModel drinkItem)
{
this.Items.Add(drinkItem);
}
}
我的XMLserializer将“Items”存储在DrinkCategory中,但不存储“Name”。
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));