C# 验证 xml 时获取架构信息

发布于 2024-12-05 10:08:33 字数 609 浏览 1 评论 0原文

我正在尝试根据架构验证一些 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

橘香 2024-12-12 10:08:33

XmlNode.SchemaInfo 似乎它会提供此信息。

我假设当使用为执行 XSD 验证而创建的 XmlReader 加载 XmlDocument 时,将填充此内容。

但是,在执行验证时,验证错误的处理程序 (XmlReaderSettings.ValidationEventHandlerXmlSchemaException 实例通过ValidationEventArgs)。特别是,输入文档中没有 XmlNode 或类似的引用。但是,有一个对 SourceSchemaObject 的引用

XmlNode.SchemaInfo seems like it will provide this information.

I assume this will be populated when an XmlDocument is loaded using an XmlReader created to perform XSD validation.

However when performing validation the handler for validation errors (XmlReaderSettings.ValidationEventHandler) there is only limited information available in the XmlSchemaException instances passed with the ValidationEventArgs). In particular there is no XmlNode or similar reference into the input document. There is however a reference to the SourceSchemaObject.

汐鸠 2024-12-12 10:08:33

我找到了解决方案。

每次调用 ValidationEventHandler 时,将 XmlSchemaValidationException.SourceObject Xmlelement 添加到列表中。验证完成后,架构信息将添加到这些对象中,使我能够访问这些信息。即Element.SchemaInfo.SchemaType.Name。

XmlSchemaValidationException 通过属性“SourceObject”(它是一个 XmlElement)传递到事件处理程序。

            List<XmlElement> errorElements = new List<XmlElement>();

            serializedObject.Validate((x, y) =>
            {
                var exception = (y.Exception as XmlSchemaValidationException);

                if (exception != null)
                {
                    var element = (exception.SourceObject as XmlElement);

                    if (element != null)
                        errorElements.Add(new XmlValidationError(element));
                }

            });

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.

            List<XmlElement> errorElements = new List<XmlElement>();

            serializedObject.Validate((x, y) =>
            {
                var exception = (y.Exception as XmlSchemaValidationException);

                if (exception != null)
                {
                    var element = (exception.SourceObject as XmlElement);

                    if (element != null)
                        errorElements.Add(new XmlValidationError(element));
                }

            });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文