属性' XmlElement'对此声明类型无效。它仅适用于< property,indexer,field,param,return'声明

时间:2014-07-15 15:32:09

标签: c# wcf

我收到错误,,,

  

属性' XmlElement'对此声明类型无效。它是   仅适用于'属性,索引器,字段,参数,返回'声明。

真正的问题

我正在尝试添加一个类作为元素而不是作为数据合同中的属性但是超出错误,无论如何,我可以制作"设置" dataContract一个xml元素: - (

[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public class FruitCrate
{
    [XmlAttribute]
    public int id;
    [XmlArray]
    public List<Setting> Settings;
}

[XmlElement]    <---- it works with    "  [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]"  just has problem with xml element but this is what I want.  
[DataContract(Name = "Settings")]
public class Setting
{
    [XmlAttribute]
    public int id;
    [XmlElement]
    public List<Fruit> Fruits;
}

1 个答案:

答案 0 :(得分:0)

固定代码,

[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public class FruitCrate
{
    [XmlAttribute]
    public int id;
    [DataMember]
    public List<Setting> Settings;
}

[DataContract(Name = "Settings")]
public class Setting
{
    [XmlAttribute]
    public int id;
    [XmlElement]
    public List<Fruit> Fruits;
}

我从[XmlArray]移除了public List<Setting> Settings;,并将其替换为[DataMember],并从[XmlElement]

上删除了public class Setting

为什么这个问题解决了我?我不知道,也许你可以回答,我会接受你的回答。