我正在做一个项目,我需要使用 datacontract 序列化来序列化枚举集合。预期结果应如下所示
<Filters>
<MemberType>1</MemberType>
<MemberType>2</MemberType>
<MemberType>3</MemberType>
</Filters>
这是要序列化的类:
[DataContract(Name = "Filters")]
public class Filters
{
private ObservableCollection<Nullable<MemberTypeEnum>> m_enmMemberType;
[DataMember(IsRequired = false, Name = "MemberType", Order = 1, EmitDefaultValue = false)]
public ObservableCollection<Nullable<MemberTypeEnum>> MemberType
{
get
{
return m_enmMemberType;
}
set
{
if (m_enmMemberType != value)
{
m_enmMemberType = value;
NotifyPropertyChanged("MemberType");
}
}
}
}
[DataContract(Name = "MemberTypeEnum")]
public enum MemberTypeEnum
{
[EnumMember(Value="1")]
[XmlEnum(Name="1")]
T_1 = 1,
[EnumMember(Value="2")]
[XmlEnum(Name="2")]
T_2 = 2,
[EnumMember(Value="3")]
[XmlEnum(Name="3")]
T_3 = 3,
}
我遇到的问题是我得到的结果略有不同:
<Filters>
<MemberType>
<MemberTypeEnum>1</MemberTypeEnum>
<MemberTypeEnum>2</MemberTypeEnum>
<MemberTypeEnum>3</MemberTypeEnum>
</MemberType>
</Filters>
有没有办法修改序列化(或者我做错了什么),以便我可以获得预期的结果?