Validate xml files present in subfolders
Hi All,
Presently I am browsing to a folder which contains xml files. futher i do validation against xsd.
But now I have to do some thing like this: When i select say folder1 in that subfolder1, subfolder2 and so on. these subfolders contains xml file which i have to validate against xsd. How do my code select files from subfolders and futher validation?
My code:
visual basic code:
Private Sub btnInputXMLFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInputXMLFile.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
mstrFolderXMLFiles = FolderBrowserDialog1.SelectedPath()
txtInputXMLFile.Text = foldername
End If
validatexmls(mstrFolderXMLFiles)
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
Try
If RadioButton2.Checked Then
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)
Dim sc As XmlSchemaCollection = New XmlSchemaCollection
AddHandler sc.ValidationEventHandler, AddressOf ValidationCallBack
sc.Add(Nothing, mstrInputXSDFile.Trim)
'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)
xmlvread.ValidationType = ValidationType.Schema
xmlvread.Schemas.Add(sc)
' 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
If m_success Then
WriterBox.Text = "No Validation Errors"
Else
MsgBox("Validation Errors")
End If
End If
Catch a As UnauthorizedAccessException
'dont have access permission
m_success = a.Message
Catch a As Exception
'and other things that could go wrong
m_success = a.Message
End Try
'The validationeventhandler is the only thing that would
'set m_Success to false
Return m_success
End Function
|