将 PSVI GetSchemaInfo 与 XSD 验证错误 ValidationEventArgs.Message 关联

发布于 2024-12-05 10:26:40 字数 1524 浏览 1 评论 0原文

我对 Extensions.GetSchemaInfo 方法 (XElement) 适用于 .Net Framework 4.0。

我想知道是否有办法从代码中

Private Sub DumpInvalidNodes(ByVal el As XElement)
   If el.GetSchemaInfo.Validity <> XmlSchemaValidity.Valid Then
     ...
   End If
End Sub

获取无效节点的 ValidationEventArgs.Message 。当您具有编号的 XML 节点(如示例中的 GrandChild1、GrandChild2)时,此示例可以正常工作。您可以按站点判断哪个无效节点正在产生验证错误。但是,当您的 XML 没有编号节点时,例如,

<Root>
    <Child>
        <GrandChild>AAA</GrandChild>
        <GrandChild>ZZZ</GrandChild>
        <GrandChild>ZZZ</GrandChild>
    </Child>
</Root>

您会得到如下所示的输出:

(1)Validating doc1 ... 
(2)The 'GrandChild' element is invalid - The value 'ZZZ' is invalid according to its datatype 'GCType' - The Enumeration constraint failed. 
(3)The 'GrandChild' element is invalid - The value 'ZZZ' is invalid according to its datatype 'GCType' - The Enumeration constraint failed. 
(4)doc1 did not validate 
(5)Invalid Element /Root 
(6)Invalid Element /Root/Child 
(7)Invalid Element /Root/Child/GrandChild 
(8)Invalid Element /Root/Child/GrandChild 

我想要一种方法来告诉第 (7) 行引用了第 (2) 行中的 ErrorMessage。

我有一个 XML 文档,我正在对其运行 XSD 验证,如本文所示,我想将 ValidationEventArgs.Message 解析为针对复杂数据类型的更用户友好的消息。我想在 DumpInvalidNodes() 方法中执行此操作,因为我可以访问无效节点,并且可以从该节点获取其他信息(例如 ID),用户可以使用这些信息来查找和修复验证问题。

I have a question about Microsoft's MSDN documentation for the Extenstions.GetSchemaInfo Method (XElement) for .Net framework 4.0.

I want to know if there's a way from within the

Private Sub DumpInvalidNodes(ByVal el As XElement)
   If el.GetSchemaInfo.Validity <> XmlSchemaValidity.Valid Then
     ...
   End If
End Sub

Code to get the ValidationEventArgs.Message for the invalid node. This example works fine when you have numbered XML nodes like in the example: GrandChild1, GrandChild2. You can tell which invalid node is producing the validation errors by site. But when you have XML without numbered nodes, e.g.

<Root>
    <Child>
        <GrandChild>AAA</GrandChild>
        <GrandChild>ZZZ</GrandChild>
        <GrandChild>ZZZ</GrandChild>
    </Child>
</Root>

you get output that looks like this:

(1)Validating doc1 ... 
(2)The 'GrandChild' element is invalid - The value 'ZZZ' is invalid according to its datatype 'GCType' - The Enumeration constraint failed. 
(3)The 'GrandChild' element is invalid - The value 'ZZZ' is invalid according to its datatype 'GCType' - The Enumeration constraint failed. 
(4)doc1 did not validate 
(5)Invalid Element /Root 
(6)Invalid Element /Root/Child 
(7)Invalid Element /Root/Child/GrandChild 
(8)Invalid Element /Root/Child/GrandChild 

I wanted a way to tell that line (7) referred to the ErrorMessage in line (2).

I have an XML document that I'm running XSD validation against as this article shows and I wanted to parse the ValidationEventArgs.Message into a more user friendly message for complex data types. I'd like to do this in the DumpInvalidNodes() method because I have access to the invalid Node and can obtain other information from the node (like an ID) that the user can use to find and fix the validation problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

不知在何时 2024-12-12 10:26:40

对您来说最简单的方法是修改原始代码,而不是在之后进行节点转储,而是在验证处理程序中进行;它会同时为您提供验证消息和节点(只需将发送者转换为 XObject 并根据需要进行处理)。

对于从文件或字符串加载的 XML,您还可以在此处使用这篇 MSDN 文章。对用户的其他提示(例如行号和列位置)也可能很有用。

The easiest way for you would be to modify the original code and instead of doing the node dumping after, do it in the validation handler; it'll give you the validation message and the node at the same time (simply cast the sender to an XObject and deal with it as you need).

For XML loaded from files or strings you could also use this MSDN article here. Additional hints to the user, such as line number and column position, could also be useful.

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