如何序列化/ C#中反序列化可选的XML枚举? [英] How to serialize/deserialize optional XML enumeration in C#?

查看:1551
本文介绍了如何序列化/ C#中反序列化可选的XML枚举?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚如何序列化/反序列化的XML上市有一个可选属性,是枚举类型C#。以下是我的C#类:

I am trying to figure out how to serialize/deserialize an XML listing to C# that has an optional attribute that is an enumerated type. The following is my C# class:

public class AttributeAssignmentExpressionElement : XACMLElement
{
    [XmlAttribute]
    public string AttributeId { get; set; }

    [XmlAttribute]
    public Category Category { get; set; }                   
}



我的类别枚举的定义如下:

public enum Category
{
    [XmlEnum(Name = "urn:oasis:names:tc:xacml:1.0:subject-category:access-subject")]
    Subject,
    [XmlEnum(Name = "urn:oasis:names:tc:xacml:3.0:attribute-category:resource")]
    Resource,
    [XmlEnum(Name = "urn:oasis:names:tc:xacml:3.0:attribute-category:action")]
    Action,
    [XmlEnum(Name = "urn:oasis:names:tc:xacml:3.0:attribute-category:environment")]        
    Environment
}  

类别出现在相应的XML文件中,作品如预期序列化/反序列化。但是,如果类别从XML丢失,则使用默认值(枚举中第一项)。如果我尽量让枚举变量可以为空(类别?),解串器,因为它是无法反序列化的复杂类型抛出异常。鉴于以下XML(不包含属性),我怎么可以适当序列化枚举

When Category is present in the corresponding XML file, serialization/deserialization works as expected. However if the Category is missing from the XML, the default value is used (first item in the enumeration). If I try to make the enumerated variable nullable (Category?), the deserializer throws an exception because it is unable to deserialize a complex type. Given the following XML (which does not contain the attribute), how can I serialize the enumeration appropriately?

<AttributeAssignmentExpression
    AttributeId="urn:oasis:names:tc:xacml:3.0:example:attribute:text">       
</AttributeAssignmentExpression>

在这种情况下,在反序列化对象的值应为null。

In this situation, the value in the deserialized object should be null.

感谢你能提供任何帮助。

Thanks for any help you can offer!

推荐答案

好了,你可以做到这一点 - 但它是有点乱:

Well, you can do this - but it is a bit messy:

[XmlIgnore]
public Category? Category { get; set; }

[XmlAttribute("Category")]
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
public Category CategorySerialized
{
    get { return Category.Value; }
    set { Category = value; }
}
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
public bool ShouldSerializeCategorySerialized()
{
    return Category.HasValue;
}

这能做什么:


  • 使用类别?用于可选的枚举值

  • 禁用类别属性序列化

  • 增加了一个辅助属性, CategorySerialized ,作为代理类别,也就是非空的和隐藏的(只要是可能的)从IDE等

  • 使用条件序列上 CategorySerialized 通过 ShouldSerialize * 模式

  • uses a Category? for the optional enum value
  • disables the Category property for serialization
  • adds a secondary property, CategorySerialized, as a proxy to Category, which is non-nullable and hidden (as far as is possible) from the IDE etc
  • use conditional serialization on CategorySerialized via the ShouldSerialize* pattern

这篇关于如何序列化/ C#中反序列化可选的XML枚举?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆