I have been working on a kitchen app and it needs to save recipes. Everything works fine with the XML Serialization except List<IngredientMeasure> IngredientList
. I have heard a lot of other people have had problems serializing lists of classes too, and I saw this: Serializing Lists of Classes to XML . I tried the solutions there, but none of them worked. Here are the fields for the Recipe
class:
[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()
{
}
}
Here is the IngredientMeasure
class:
public class IngredientMeasure
{
[DataMember]
public Ingredient ingredient;
[DataMember]
public Measure measure;
public IngredientMeasure()
{
}
}
Any help is appreciated.
From the link you just mentioned, XmlArray
should be on the list of contained items not on the list of string.
[DataContract]
public class Recipe
{
[XmlArray("IngredientsList", XmlArrayItem(typeof(IngredientMeasure), ElementName = "IngredientMeasure"))]
public List<IngredientMeasure> IngredientList { get; set; }
// ... other properties
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.