xml反序列化期间如何忽略无效的枚举值? [英] How to ignore invalid enum values during xml deserialization?

查看:74
本文介绍了xml反序列化期间如何忽略无效的枚举值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将xml文档反序列化为一个类,该类由相关的xsd文件生成.我无法控制xml文件的内容.

I want to deserialize an xml document to a class, which is genereated by the concerning xsd files. I don't have control over the contents of the xml-file.

在反序列化期间,我遇到了一个异常,因为xml文档中的枚举值不符合xsd的要求.我不希望中断,而是希望反序列化继续进行,并且仅对任何此类错误采用默认值.有什么方法可以完成此行为?

During deserialization I run into an exception, because an enum value in the xml document does not meet the requiremnts of the xsd. Instead of breaking, i would like the deserialization to continue and just take the default value for any such errors. Is there any way to accomplish this behaviour?

为了澄清起见,我想达到的目的:我想从数字发票中读取数据.因此,xml文件的创建是某种黑盒,即使结构符合标准,也可能包含flase值.但这并不意味着每个值都存在这种缺陷.异常阻止我读取正确的值,所以我只希望通过反序列化来完成,如果发生这样的错误,则以某种方式插入默认值.

edit: For clarification, what i am trying to achieve: I want to read data from digital invoices. So the creation of the xml file is some kind of blackbox and can contain possibly flase values, even if the structure meets the standards. But that does not mean, that every value is flawed in that way. The exception prevents me from reading the correct values so i just want the deserialization to finish by somehow inserting the default values if such an error occurs.

既不能将值标记为过时,也不能用XmlIgnore标记值,因为我收到的下一个xml可能包含正确的值.

Neither marking the values as obsolete, nor flagging them with XmlIgnore won't work, because the next xml i receive could contain correct values.

我希望这有助于澄清问题.

I hope that helped clarifying the problem.

现在,我正在使用 System.Xml.Serialization dll,但是我愿意实现任何可以帮助我实现所需行为的库.

Right now, im using the System.Xml.Serialization dll, but im willing to implement any library which can help me achieve the wanted behaviour.

该异常立即出现:

"System.InvalidOperationException:实例验证错误:'x'是不是xType的有效值."

"System.InvalidOperationException: Instance validation error: 'x' is not a valid value for xType.."

引发异常的代码:

XmlSerializer serializer = new xml.XmlSerializer(typeof(MyType));
MyType invoice = serializer.Deserialize(memoryStream) as MyType;

我知道代码并没有太大帮助,所以我将添加当前有问题的枚举:

I know the code does not help very much, so I'll add the enum, that is currently problematic:

public enum PaymentMeansCodeContentType
    {

        [System.Xml.Serialization.XmlEnumAttribute("10")]
        Item10,

        [System.Xml.Serialization.XmlEnumAttribute("20")]
        Item20,

        [System.Xml.Serialization.XmlEnumAttribute("30")]
        Item30,

        [System.Xml.Serialization.XmlEnumAttribute("48")]
        Item48,

        [System.Xml.Serialization.XmlEnumAttribute("49")]
        Item49,

        [System.Xml.Serialization.XmlEnumAttribute("57")]
        Item57,

        [System.Xml.Serialization.XmlEnumAttribute("58")]
        Item58,

        [System.Xml.Serialization.XmlEnumAttribute("59")]
        Item59,

        ZZZ,
    }

这些是使用xsd命令行工具自动生成的: https://docs.microsoft.com/de-de/dotnet/standard/serialization/xml-schema-definition-tool-xsd-exe

These are autogenerated from using the xsd command line tool: https://docs.microsoft.com/de-de/dotnet/standard/serialization/xml-schema-definition-tool-xsd-exe

我需要反序列化的xml为我提供了"1",因此显然是无效值.我仍然需要从xml访问其他有效值,并提供用于指示哪些值有缺陷的方法.

The xml i need to deserialize provides me with a '1', so clearly an invalid value. Still i need to access the other valid values from the xml and provide means for indicating which values are flawed.

推荐答案

我仍然无法找到我想要的简单答案,但是设法找到了适合我的解决方案.我最终针对可能的值预先验证了XML文件中的每个枚举.如果XML与枚举不匹配,我将错误的值和节点保存到验证结果集中,并用枚举默认值覆盖xml.

I still wasn't able to find the simple answer I was hoping for but managed to find a work around that worked for me. I ended up validating every enum in the XML file beforehand against the possible values. If the XML did not match the enum i saved the wrong value and node to a validation result set and overwrote the xml with the enum default value.

这篇关于xml反序列化期间如何忽略无效的枚举值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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