使用XmlSchemaValidator验证XmlElement,当xsi:nil=true时,使用专门的重载方法
2011-06-18 07:03
537 查看
当一个元素定义为可空nillable=true, 实际Xml节点xsi:nil=true, 验证时还是报内容无效,不能为空。
_validator.ValidateEndElement(null);
http://connect.microsoft.com/VisualStudio/feedback/details/97503/xmlschemavalidator-incorrectly-allows-xsi-nil-true-on-an-element-whose-nillable-is-false
Q:
XmlSchemaValidator should not allow xsi:nil="true" on those xs:element whose nillable is false.
I tried some. 1) setting IsNil = true on XmlSchemaInfo which is being passed to ValidateElement() and so on. 2) call ValidateAttribute() with "nil" as localName and XmlSchema.InstanceNamespace as namespace URI.
A:
Thank you very much for your feedback. The issue you are running into is that nillabiliy is a constraint on the element not on the xsi:nill attribute itself. To validate the the element is appropriately set to nil or not you can use the overloaded ValidateElement method:
void ValidateElement(string localName, string namespaceURI, XmlSchemaInfo schemaInfo, string xsiType, string xsiNil, string xsiSchemaLocation, string xsiNoNamespaceSchemaLocation);
Validating the attribute xsi:nil is really just verifying whether one of the correct values of nil were provided.
Here is a background comment from a developer on this:
This is because, the value of the xsi attributes can even change the declaration we look up for the element. For example, xsi:type attribute can change the type of the element to a sub or super type. Even though xsi:nil does not change the element declaration, to be consistent, all xsi attribute processing is done via this overload and subsequent ValdiateAttribute calls on the xsi:attributes are ignored.
Thanks you again for this feedback. I will flag this bug for documentation to try to make the api clearer in this area. Thanks for your help!
_validator.ValidateEndElement(null);
http://connect.microsoft.com/VisualStudio/feedback/details/97503/xmlschemavalidator-incorrectly-allows-xsi-nil-true-on-an-element-whose-nillable-is-false
Q:
XmlSchemaValidator should not allow xsi:nil="true" on those xs:element whose nillable is false.
I tried some. 1) setting IsNil = true on XmlSchemaInfo which is being passed to ValidateElement() and so on. 2) call ValidateAttribute() with "nil" as localName and XmlSchema.InstanceNamespace as namespace URI.
A:
Thank you very much for your feedback. The issue you are running into is that nillabiliy is a constraint on the element not on the xsi:nill attribute itself. To validate the the element is appropriately set to nil or not you can use the overloaded ValidateElement method:
void ValidateElement(string localName, string namespaceURI, XmlSchemaInfo schemaInfo, string xsiType, string xsiNil, string xsiSchemaLocation, string xsiNoNamespaceSchemaLocation);
Validating the attribute xsi:nil is really just verifying whether one of the correct values of nil were provided.
Here is a background comment from a developer on this:
This is because, the value of the xsi attributes can even change the declaration we look up for the element. For example, xsi:type attribute can change the type of the element to a sub or super type. Even though xsi:nil does not change the element declaration, to be consistent, all xsi attribute processing is done via this overload and subsequent ValdiateAttribute calls on the xsi:attributes are ignored.
Thanks you again for this feedback. I will flag this bug for documentation to try to make the api clearer in this area. Thanks for your help!
相关文章推荐
- javascript中使用ajax异步验证数据库后方法return true时总是返回undefined
- 使用hibernate-validator进行验证时与tomcat产生的方法冲突问题【NoSuchMethodError】
- 【转载】C#验证控件的使用方法(RegularExpressionValidator)的使用
- javascript中使用ajax异步验证数据库后方法return true时总是返回undefined
- 表单验证ASP.NET RequiredFieldValidator 控件使用方法及使用实例
- 解决Struts1.x中使用Validator框架对DispatchAction不同方法使用不同的字段验证
- bootstrapValidator表单验证使用方法
- [ZT]C#验证控件的使用方法(RegularExpressionValidator)的使用
- jQuery.validator.addMethod自定义验证方法【在表单验证中的使用 $("#appEdit_Form").validate({rules : {},messages:{}】
- 如何优雅的使用 laravel 的 validator验证方法
- ASP.NET中Textbox后的必填验证控件RequiredFieldValidator的使用方法。
- jQuery表单验证插件Validator的使用方法
- 解决Struts1.x中使用Validator框架对DispatchAction不同方法使用不同的字段验证
- Struts2使用validator验证框架的问题 只验证部分方法
- [置顶] SpringMVC 之 Validation介绍—— 使用Validator接口进行验证
- php使用preg_match()函数验证ip地址的方法
- C#验证控件的使用方法
- JFinal validator一个校验类对应多个验证的方法
- 使用 jQuery 验证密码一致性,正则表达式验证邮箱及手机号的方法
- Struts2教程4:使用validate方法验证数据