如何将可枚举的枚举序列化为属性

时间:2013-02-05 23:48:21

标签: c# xml-serialization

我有一个我需要从xml反序列化的类,它有一个枚举属性,存储为xml中的属性。有时可能会丢失此属性或将“”作为值。 如何使序列化程序处理使BorrowerResidencyType属性可以为空?

XML:

<_RESIDENCE _StreetAddress="" _City="San Jose" _State="CA" BorrowerResidencyType="" />
<_RESIDENCE _StreetAddress="" _City="San Jose" _State="CA"  />

C#:

[System.CodeDom.Compiler.GeneratedCodeAttribute ( "System.Xml", "4.0.30319.17929" )]
[System.SerializableAttribute ()]
[System.Xml.Serialization.XmlTypeAttribute ( AnonymousType = true )]
public enum _RESIDENCEBorrowerResidencyType
{

    /// <remarks/>
    Current,

    /// <remarks/>
    Prior,
}

public class Test{
public string StreetAddress{get;set;}
public string City{get;set;}
[System.Xml.Serialization.XmlAttributeAttribute ()]
public _RESIDENCEBorrowerResidencyType BorrowerResidencyType{get;set;}
}

是否有另一个能够更智能地处理这种情况的库?

1 个答案:

答案 0 :(得分:3)

可能是这样的:

public enum _RESIDENCEBorrowerResidencyType
{
    [XmlEnum(Name="")]
    Default = 0,

    Current,
    Prior
}