开发者

Relate PSVI GetSchemaInfo to the XSD Validation Error ValidationEventArgs.Message

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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜