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

2024-01-25

我在下面的类中收到以下错误:

无法序列化 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; }
    }

}

你可能需要使用[XmlElement]代替[XmlAttribute]。属性不能是复杂类型。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

XmlAttribute/XmlText 不能用于编码复杂类型 的相关文章

随机推荐