首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >序列化为XML时的XmlChoiceIdentifier和List问题

序列化为XML时的XmlChoiceIdentifier和List问题
EN

Stack Overflow用户
提问于 2014-12-10 11:20:21
回答 1查看 1.9K关注 0票数 2

我使用Xsd2Code从XSD模式生成C#类。

在模式中,我有以下节选:

代码语言:javascript
运行
复制
<hcparty>
  <firstname>some value</firstname>
  <familyname>some other value</familyname>
</hcparty>

该工具制作了以下课程:

代码语言:javascript
运行
复制
[Serializable]
public class hcpartyType
{

    private List<string> itemsField;

    private List<ItemsChoiceType> itemsElementNameField;

    /// <summary>
    /// hcpartyType class constructor
    /// </summary>
    public hcpartyType()
    {
        itemsElementNameField = new List<ItemsChoiceType>();
        itemsField = new List<string>();
    }

    //[XmlArrayItem(typeof(ItemChoiceType))]
    [XmlChoiceIdentifier("ItemsElementName")]
    public List<string> Items
    {
        get
        {
            return itemsField;
        }
        set
        {
            itemsField = value;
        }
    }

    [XmlIgnore()]
    public List<ItemsChoiceType> ItemsElementName
    {
        get
        {
            return itemsElementNameField;
        }
        set
        {
            itemsElementNameField = value;
        }
    }      
}

public enum ItemsChoiceType
{

    /// <remarks/>
    familyname,

    /// <remarks/>
    firstname,

    /// <remarks/>
    name,
}

首先,我必须添加可序列化的类装饰,因为它丢失了。

当序列化到XML时,我会得到以下错误:

代码语言:javascript
运行
复制
Type of choice identifier 'ItemsElementName' is inconsistent with type of 'Items'. Please use array of System.Collections.Generic.List`1[[MyNamespace.ItemsChoiceType, ...]].

好吧,所以我说:

代码语言:javascript
运行
复制
[XmlArrayItem(typeof(ItemChoiceType))]

在上面的代码中,我对它进行了注释。我想是在合适的地方。错误仍然存在。

我阅读了下面的链接,所以我想知道这个bug是否仍然适用,我必须将我的列表更改为Array。

有相同设计问题的人吗?关于我的案子的博客文章

XmlSerializer与XmlChoiceIdentifier的问题

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-10-10 07:09:18

Xml序列化程序期望XmlChoiceIdentifier成员的类型是数组。不支持列表。

尝试以下几点:

代码语言:javascript
运行
复制
[Serializable]
public class hcpartyType
{

    private List<string> itemsField;

    private List<ItemsChoiceType> itemsElementNameField;

    [XmlChoiceIdentifier("ItemsElementName")]
    public string[] Items
    {
        get
        {
            return itemsField;
        }
        set
        {
            itemsField = value;
        }
    }

    [XmlIgnore()]
    public ItemsChoiceType[] ItemsElementName
    {
        get
        {
            return itemsElementNameField;
        }
        set
        {
            itemsElementNameField = value;
        }
    }      
}

public enum ItemsChoiceType
{

    /// <remarks/>
    familyname,

    /// <remarks/>
    firstname,

    /// <remarks/>
    name,
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27399714

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档