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

标签 complextype xml-attribute

我在以下类(class)中收到以下错误:

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

}

最佳答案

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

关于complextype - XmlAttribute/XmlText 不能用于编码复杂类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16759748/

相关文章:

apache-spark - Spark 高阶函数转换输出结构

clr - 可以安全地将 Numeric.Complex 与 PInvoke 一起使用吗? (它没有 LayoutKind.Sequential)

c# - 复杂类型的方法参数验证

xml-parsing - 如何用冒号解码 XML 属性?

jaxb - 编码时可选的 JAXB xml 属性

xhtml - 为什么我的文档没有验证为 XHTML 1.1 STRICT?

wcf - 在 SOAP 消息中将复杂类型作为参数发送

c# - ASP.NET 动态数据编辑复杂对象

c# - 将 XML 属性与 IntermediateSerializer 一起使用

javascript - 如何使用 JavaScript 变量作为 XHTML 属性值?