Windows 通用 XML 序列化
本文关键字:序列化 XML 通用 Windows | 更新日期: 2023-09-27 18:36:43
我一直在开发一个厨房应用程序,它需要保存食谱。除了 List<IngredientMeasure> IngredientList
之外,XML 序列化一切正常。我听说很多其他人在序列化类列表时也遇到了问题,我看到了这个:将类列表序列化为 XML。我在那里尝试了解决方案,但没有一个奏效。以下是Recipe
类的字段:
[DataContract]
public class Recipe
{
[DataMember]
public static List<Recipe> RecipeList = new List<Recipe>();
[DataMember]
public string Name { get; set; }
[DataMember]
public string FilePath { get; set; }
[DataMember]
public List<IngredientMeasure> IngredientList { get; set; }
[XmlArray("IngredientsList")]
public List<string> Ingredients = new List<string>();
[DataMember]
public List<string> DirectionList { get; private set; }
[DataMember]
public List<string> Notes { get; private set; }
public Recipe()
{
}
}
这是IngredientMeasure
类:
public class IngredientMeasure
{
[DataMember]
public Ingredient ingredient;
[DataMember]
public Measure measure;
public IngredientMeasure()
{
}
}
任何帮助,不胜感激。
从您刚才提到的链接中, XmlArray
应该在不包含字符串列表中的包含项目列表中。
[DataContract]
public class Recipe
{
[XmlArray("IngredientsList", XmlArrayItem(typeof(IngredientMeasure), ElementName = "IngredientMeasure"))]
public List<IngredientMeasure> IngredientList { get; set; }
// ... other properties
}