XmlAttribute / XmlText不能用于编码复杂类型

时间:2013-05-26 13:34:43

标签: complextype xml-attribute

我在下面的课程中遇到以下错误:

无法序列化DataObjects.Ingredient类型的成员'Ingredient'。 XmlAttribute / XmlText不能用于编码复杂类型。

有关为何的想法?

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

}

2 个答案:

答案 0 :(得分:34)

您可能必须使用[XmlElement]而不是[XmlAttribute]。属性不能是复杂类型。

答案 1 :(得分:1)

另一个建议:在根元素下面省略(list)属性的前缀。