XmlAttribute/XmlText cannot be used to encode complex types
Asked Answered
H

2

19

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

}
Hyo answered 26/5, 2013 at 13:34 Comment(0)
S
59

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

Seymour answered 31/7, 2013 at 12:29 Comment(0)
M
2

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

Meek answered 31/7, 2013 at 21:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.