C# Xml 反序列化加上设计建议 [英] C# Xml Deserialize plus design suggestions

查看:34
本文介绍了C# Xml 反序列化加上设计建议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,我需要构建一个应该向后兼容的通用反序列化器.示例:XML 看起来像

In my project I need to build a generic deserializer that should be backward compatible. Example: The XML looks like

<PolicyDef name = "sample" type="type1">
  <Options ......>
</PolicyDef>

类型"是枚举 - PolicyTypes例如

The "type" is enum - PolicyTypes e.g

public Enum PolicyTypes
{
 type1 = 0,
 type2 = 1
}

PolicyDef 类定义为

The PolicyDef class is defined as

[XmlRoot("PolicyDef")]
    public class PolicyDef
    {
        private string policyName;
        private PolicyTypes policyType;

        public PolicyDefinition()
        {         
        }

        [XmlAttribute]
        public string Name
        {
            get
            {
                return this.policyName;
            }
            set
            {
               this.policyName = value;

            }
        }      
        [XmlAttribute]
        public PolicyTypes Type
        {
            get
            {
                return this.policyType;
            }
            set
            {
               this.policyType = value;

            }
        }           
    }

这种方法的问题是,如果稍后我放入除类型 1 或类型 2 之外的任何类型,XMLDeserializer 将抛出异常.所以如果我有像

The Problem with this approach is that if later on I put any type other than type 1 or type 2, the XMLDeserializer will throw exception. so if i have the xml like

<PolicyDef name = "sample" type="type_new">
  <Options ......>
</PolicyDef>

反序列化器将抛出错误,因为 type_new 无效.

The deserializer will throw error as type_new not valid.

我想知道是否有办法钩入反序列化器进程来捕获它并设置默认值而不是抛出错误.说如果有任何无效值,那么我会将其设置为type1"

I was wondering if there is a way to hook into the deserializer process to catch that and set a default value rather than throw error. Say if there is any invalid value, then I would set that to "type1"

或者我愿意接受有关如何处理此问题的建议

Or am open to suggestions regarding how to handle this problem

感谢和问候

推荐答案

感谢 Chris 的建议,但我不希望最终编写完整的解析代码,如果 XML 和相应的类庞大而复杂,这可能会很混乱.无论如何,我使用了不同的方法.我将所有枚举字段更改为字符串.在这种情况下,不会有解析错误,然后公开另一个属性,该属性会将解析值作为枚举返回,如果解析失败,则返回默认枚举值.例如

Thanks Chris for the suggestion, but I don't want end up writing the complete parsing code which could be messy if the XML and corresponding class is huge and complex. I anyway used a different approach. I changed all the enum fields to string. In this case there would be no parsing error and then expose another property that would return the parsed value as enum and if the parsing fails, then return default enum value. E.g

private string policyName;
[XmlAttribute("Type")]
public string Type
{
    private get
    {
        return this.policyType;
    }
    set
    {
        this.policyType = value;
        try
        {
            this.PolicyType = (PolicyTypes)Enum.Parse(typeof(PolicyTypes), this.policyType);
        }
        catch(Exception)
        {
            this.PolicyType = PolicyTypes.DefaultPolicy;
        }
    }
}

public PolicyTypes PolicyType
{
    get;
    private set;
}

并使用 class 属性访问值而不是 xml 属性字段.

And use the class property to access the value rather than the xml attribute field.

这篇关于C# Xml 反序列化加上设计建议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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