2013-05-26 144 views
9

我得到的下面类以下错误:XmlAttribute/XMLTEXT不能用于编码复杂类型

不能键入DataObjects.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; } 
    } 

} 

回答

27

你可能会使用[XmlElement]而不是[XmlAttribute]。属性不能是复杂的类型。

1

另一个建议:漏下根元素下面(列表)属性的前缀。