Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XmlAttribute/XmlText cannot be used to encode complex types

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; }
    }

}
like image 700
David Poxon Avatar asked May 26 '13 13:05

David Poxon


2 Answers

You will probably have to use [XmlElement] instead of [XmlAttribute]. An attribute cannot be a complex type.

like image 104
user2366741 Avatar answered Nov 04 '22 08:11

user2366741


Another suggestion: leave out the prefix for (list ) properties below the root element.

like image 2
Peter Klein Avatar answered Nov 04 '22 09:11

Peter Klein