Validating Xml files with XSD file
Hi,
After selecting the folder which contains xml files gets validate and display errors in RichTextBox.
Here it showing error from one xml file, but there is one more xml file which contains error.
How would I display error from multiple xml files in richtextbox.
Private Sub btnFolderXMLFiles_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFolderXMLFiles.Click
Dim foldername As String
Dim filename As String
If FolderBrowserDialog1.ShowDialog() = DialogResult.OK Then
foldername = FolderBrowserDialog1.SelectedPath()
Else
Dim Results As DialogResult = FolderBrowserDialog1.ShowDialog
If Results = DialogResult.Cancel Then
Return
End If
Exit Sub
End If
txtFolderXMLFiles.Text = foldername
validatexmls(foldername)
End Sub
Private Function validatexmls(ByVal infile As String) As Boolean
Dim filename As String
Dim mydir As New DirectoryInfo(infile)
Dim m_xmld As XmlDocument
Dim f As FileInfo() = mydir.GetFiles("*.xml")
Dim i, j As Integer
For i = 0 To f.Length - 1
filename = f(i).FullName
' create xml doc
m_xmld = New XmlDocument
m_xmld.Load(filename)
'First we create the xmltextreader
Dim xmlr As New XmlTextReader(filename)
'We pass the xmltextreader into the xmlvalidatingreader
'This will validate the xml doc with the schema file
'NOTE the xml file it self points to the schema file
Dim xmlvread As New XmlValidatingReader(xmlr)
' Set the validation event handler
AddHandler xmlvread.ValidationEventHandler, AddressOf ValidationCallBack
m_success = True 'make sure to reset the success var
' Read XML data
While (xmlvread.Read)
End While
'Close the reader.
xmlvread.Close()
Next
'The validationeventhandler is the only thing that would
'set m_Success to false
Return m_success
End Function
Private Sub ValidationCallBack(ByVal sender As Object, ByVal args As ValidationEventArgs)
'Display the validation error. This is only called on error
m_success = False
Dim mywriter As System.IO.TextWriter
mywriter = System.IO.File.CreateText("ValidationError.log")
mywriter.WriteLine("Validation error: " + args.Message)
mywriter.Close()
WriterBox.Text = ("Validation error: " + args.Message)
End Sub
Thanks,
Shailesh
|