|
-
Mar 1st, 2012, 06:29 PM
#1
Thread Starter
Hyperactive Member
Validation of XML against XSD, and ValidationEventHandler
So the below code validates test.xml against validate.xsd (found the sample code in these forums).
Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim myDocument As New XmlDocument
Dim lsFile As String
lsFile = "C:\test.xml"
myDocument.Load(lsFile)
myDocument.Schemas.Add("", "c:\validate.xsd")
myDocument.Validate(AddressOf ValidationEventHandler)
MsgBox(myDocument.DocumentElement.ChildNodes(0).Name)
End Sub
Private Sub ValidationEventHandler(ByVal sender As Object, ByVal e As ValidationEventArgs)
Select Case e.Severity
Case XmlSeverityType.Error
TextBox1.Text = TextBox1.Text & vbNewLine & "Error: " & e.Message
Case XmlSeverityType.Warning
TextBox1.Text = TextBox1.Text & vbNewLine & "Warning: " & e.Message
End Select
For every error it finds, ValidationEventHandler is fired off and a line describing the error is added to the text box.
My questions are
1) How do I access the XMLElement object that was found to be invalid against the XSD, inside the ValidationEventHandler?
For example test.xml has:
<File>
<Item>
<ID>1232231</ID>
<Date>xx</Date>
</Item>
...
</File>
<Date> is invalid against validate.xsd, because it was expecting a real date. So ValidationEventHandler is triggered.
Inside the ValidationEventHandler, how can I get access to that <Date> XMLElement (or whatever element that was invalid)?
I want to access <Date>'s parent node, so I can access <ID> and figure out where this error occurred.
2) Is there a way to pass parameters into ValidationEventHandler from Form1_Load? (Like say I want to pass it an Array to store all the validation errors it finds).
3) Currently exception.linenumber does not work (it returns 0). Is there a way to get the line number the error was found?
"I like to run on treadmills, because at least I know I'm getting nowhere."
- Me
-
Mar 1st, 2012, 09:31 PM
#2
Re: Validation of XML against XSD, and ValidationEventHandler
Can you post part of your XML file, and the XSD?
That is the very essence of human beings and our very unique capability to perform complex reasoning and actually use our perception to further our understanding of things. We like to solve problems. -Kleinma
Does your code in post #46 look like my code in #45? No, it doesn't. Therefore, wrong is how it looks. - jmcilhinney
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|