I am getting the following error on the below class:
Cannot serialize member 'Ingredient' of type DataObjects.Ingredient. XmlAttribute/XmlText cannot be used to encode complex types.
Any thoughts as to why?
[DataContract]
[Serializable]
[XmlRoot("ingredient")]
public class Ingredient
{
private string id;
private string name;
private string description;
private IngredientNutrient[] nutrients;
public Ingredient(string id, string name, string description, IngredientNutrient[] nutrients)
{
this.id = id;
this.name = name;
this.description = description;
this.nutrients = nutrients;
}
public Ingredient(string id, string name, string description)
{
this.id = id;
this.name = name;
this.description = description;
}
public Ingredient()
{
}
[DataMember]
[XmlAttribute("id")]
public string ID
{
get { return this.id; }
set { this.id = value; }
}
[DataMember]
[XmlAttribute("name")]
public string Name
{
get { return this.name; }
set { this.name = value; }
}
[DataMember]
[XmlAttribute("description")]
public string Description
{
get { return this.description; }
set { this.description = value; }
}
[DataMember]
[XmlArray("nutrients")]
[XmlArrayItem("ingredientnutrient")]
public IngredientNutrient[] Nutrients
{
get { return this.nutrients; }
set { this.nutrients = value; }
}
}
You will probably have to use [XmlElement]
instead of [XmlAttribute]
. An attribute cannot be a complex type.
Another suggestion: leave out the prefix for (list ) properties below the root element.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With