C# 验证 xml 时获取架构信息
我正在尝试根据架构验证一些 XML,并收集尽可能多的信息,以便为用户提供有价值的错误消息。
我已成功根据 XSD 验证序列化对象。对于所有错误,我的 ValidationEventHandler 都被正确调用,我在那里得到了一些信息。唯一的问题是架构信息此时不可用 - 我正在尝试获取元素的架构类型。即给定以下模式元素,我想获得“BookType”,
<element minOccurs="0" maxOccurs="1" name="TypeOfBook" type="myTypes:BookType" />
我相信模式/验证信息在验证过程中被插入到 xml 中。因此,如果我连续两次调用 validate,仅在第二次处理错误,则架构信息可用。
serializedObject.Validate((x, y) => { });
serializedObject.Validate((x, y) => { // handle errors here because elements will have schema info available });
显然,这个解决方案还有很多不足之处。处理这个问题的推荐方法是什么?
I'm trying to validate some XML against a schema and gather as much information as possible to provide valuable error messages to the user.
I've managed to validate a serialized object against an XSD. My ValidationEventHandler is being called properly for all errors and I get get some information there. The only problem is that schema information is not available at this point - I'm trying to get to the schema type of the element. i.e. given the following schema element, I would like to get "BookType"
<element minOccurs="0" maxOccurs="1" name="TypeOfBook" type="myTypes:BookType" />
I believe schema/validation information is being inserted into the xml during the validation process. So if I call validate twice in a row, only handling the erros the second time around, the schema information is available.
serializedObject.Validate((x, y) => { });
serializedObject.Validate((x, y) => { // handle errors here because elements will have schema info available });
Obviously, this solution leaves much to be desired. What is the recommended way of dealing with this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
XmlNode.SchemaInfo
似乎它会提供此信息。我假设当使用为执行 XSD 验证而创建的
XmlReader
加载XmlDocument
时,将填充此内容。但是,在执行验证时,验证错误的处理程序 (
XmlReaderSettings.ValidationEventHandler
)XmlSchemaException
实例通过ValidationEventArgs
)。特别是,输入文档中没有XmlNode
或类似的引用。但是,有一个对SourceSchemaObject 的引用
。XmlNode.SchemaInfo
seems like it will provide this information.I assume this will be populated when an
XmlDocument
is loaded using anXmlReader
created to perform XSD validation.However when performing validation the handler for validation errors (
XmlReaderSettings.ValidationEventHandler
) there is only limited information available in theXmlSchemaException
instances passed with theValidationEventArgs
). In particular there is noXmlNode
or similar reference into the input document. There is however a reference to theSourceSchemaObject
.我找到了解决方案。
每次调用 ValidationEventHandler 时,将 XmlSchemaValidationException.SourceObject Xmlelement 添加到列表中。验证完成后,架构信息将添加到这些对象中,使我能够访问这些信息。即Element.SchemaInfo.SchemaType.Name。
XmlSchemaValidationException 通过属性“SourceObject”(它是一个 XmlElement)传递到事件处理程序。
I found a solution.
Every time the ValidationEventHandler gets called, add the XmlSchemaValidationException.SourceObject Xmlelement to a list. Once validation is complete the schema information would be added to these objects, enabling me to access the information. i.e. Element.SchemaInfo.SchemaType.Name.
An XmlSchemaValidationException gets passed to the event handler with a property "SourceObject " which is an XmlElement.