1

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.

Community
  • 1
  • 1
Alerik
  • 25
  • 5

1 Answers1

1

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 
}
Fab
  • 14,327
  • 5
  • 49
  • 68