带枚举的XmlSerializer和XML验证 [英] XmlSerializer and XML validation with enumerations

查看:75
本文介绍了带枚举的XmlSerializer和XML验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在序列化包含未在对象的XSD中定义的枚举值的对象时出现问题。这是错误:

I'm having an issue serializing an object that contains an enumeration value that is not defined in the object's XSD. Here is the error:

System.InvalidOperationException:生成XML文档时出错。 ---> System.InvalidOperationException:实例验证错误:'10000'不是VehicleMasterStatus的有效值。

System.InvalidOperationException: There was an error generating the XML document. ---> System.InvalidOperationException: Instance validation error: '10000' is not a valid value for VehicleMasterStatus.

奇怪的是我在项目的任何地方都没有包含类的模式。这些类是从XSD创建的,所以我想也许类定义中的属性可能是XmlSerializer使用
来"验证"。作为序列化,但这只是推测。

The odd thing is that I do not have the schemas for the containing class anywhere in my project. The classes were created from the XSDs, so I'm thinking maybe there are attributes on the properties within the class definition that the XmlSerializer uses to "validate" as its serializing, but this is just speculation.

当我调用XmlSerializer.Serializer(stream,object)时发生错误。我自己没有对任何XML验证做任何想法。

The error is occurring when I call XmlSerializer.Serializer(stream, object). Not doing anything fancy with any XML validation myself.

我的问题是:XmlSerializer是否进行验证,因为它将对象序列化为XML?并且,如果是这样,我可以做些什么来防止它抱怨我的枚举属性中包含的值而不修改XSD或类
定义(我无权访问)。

My question is this: Does the XmlSerializer do validation as it is serializing an object to XML? And, if so, is there something I can do to prevent it from complaining about the value contained in my enumeration property without modifying the XSD's or class definition (which I do not have access to).

谢谢

推荐答案

没有与xsd相关的验证,但在序列化过程中,序列化程序可能会搜索相关的枚举元素名称。在你的情况下,它不存在。您可以通过在此处发布完整的堆栈跟踪来验证它。

there is no xsd-related validation but in the serialziation process the serializer might search for the relevant enum element name. in your case it does not exist. you can verify it by publishing here the full stack trace.

还在c#中发布枚举定义。

also publish the enum definitain in c#.

所以即使xsd不存在也是如此c#中的枚举定义是从它创建的,它仍然存在。

so even when the xsd is not present the enum definition in c# was created from it and it still exists.


这篇关于带枚举的XmlSerializer和XML验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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