使用XmlSchemaValidator验证XmlElement,当xsi:nil=true时,使用专门的重载方法

当一个元素定义为可空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!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值