XmlAttribute/XmlText不能用于编码复杂类型 [英] XmlAttribute/XmlText cannot be used to encode complex types

查看:221
本文介绍了XmlAttribute/XmlText不能用于编码复杂类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在以下课程上遇到以下错误:

I am getting the following error on the below class:

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

Cannot serialize member 'Ingredient' of type DataObjects.Ingredient. XmlAttribute/XmlText cannot be used to encode complex types.

有什么想法吗?

[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].属性不能是复杂类型.

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

这篇关于XmlAttribute/XmlText不能用于编码复杂类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆