我使用Xsd2Code从XSD模式生成C#类。
在模式中,我有以下节选:
<hcparty>
<firstname>some value</firstname>
<familyname>some other value</familyname>
</hcparty>
该工具制作了以下课程:
[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时,我会得到以下错误:
Type of choice identifier 'ItemsElementName' is inconsistent with type of 'Items'. Please use array of System.Collections.Generic.List`1[[MyNamespace.ItemsChoiceType, ...]].
好吧,所以我说:
[XmlArrayItem(typeof(ItemChoiceType))]
在上面的代码中,我对它进行了注释。我想是在合适的地方。错误仍然存在。
我阅读了下面的链接,所以我想知道这个bug是否仍然适用,我必须将我的列表更改为Array。
有相同设计问题的人吗?关于我的案子的博客文章
发布于 2017-10-10 07:09:18
Xml序列化程序期望XmlChoiceIdentifier成员的类型是数组。不支持列表。
尝试以下几点:
[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,
}
https://stackoverflow.com/questions/27399714
复制相似问题