Hi
I have the following piece of code for an ASP.NET page which is causing problems.:
The code simply loops through the XML files in a folder, extracts the schema location, determines whether the XML is well formed and then displays a table to the user.
Aside from the fact that it's pretty messy, it seems to be leaving all the files in use. I'm not sure why. Has anyone got any ideas what I'm not closing or what I'm doing wrong?
Thanks for any help.
Chris
I have the following piece of code for an ASP.NET page which is causing problems.:
Code:
Public Function ValidateFiles(ByVal sourcedir As String)
If sourcedir <> "" Then
Dim file As String
Dim tblFiles As New Table
tblFiles.CellPadding = 5
tblFiles.BorderWidth = Unit.Pixel(1)
Dim tblHeadCell As New TableHeaderCell
Dim tblHeadCell2 As New TableHeaderCell
Dim tblHeadCell3 As New TableHeaderCell
Dim tblHeadRow As New TableRow
tblHeadCell.Text = "File Name"
tblHeadCell2.Text = "Schema Location"
tblHeadCell3.Text = "Message"
tblHeadRow.Controls.Add(tblHeadCell)
tblHeadRow.Controls.Add(tblHeadCell2)
tblHeadRow.Controls.Add(tblHeadCell3)
tblFiles.Controls.Add(tblHeadRow)
For Each file In GetFiles(sourcedir)
Dim fileinfo As New FileInfo(file.ToString)
Dim internalschema As String
If fileinfo.Extension = ".xml" Then
Dim tr As XmlTextReader = New XmlTextReader(file.ToString)
Dim vr As XmlValidatingReader = New XmlValidatingReader(tr)
Try
vr.ValidationType = ValidationType.None
While (vr.Read())
If vr.Name.Equals("Root") And vr.AttributeCount > 0 Then
internalschema = New String(vr.GetAttribute("xsi:schemaLocation"))
End If
End While
Dim tblRow As New TableRow
Dim tblFileCell As New TableCell
tblfilecell.BackColor = Color.LightGray
tblfilecell.Font.Bold = True
tblFileCell.Text = fileinfo.Name
tblRow.Controls.Add(tblFileCell)
Dim tblSchemaCell As New TableCell
tblSchemaCell.BackColor = Color.LightGray
tblSchemaCell.Font.Bold = True
tblSchemaCell.Text = internalschema.ToString
tblRow.Controls.Add(tblSchemaCell)
Dim tblMessageCell As New TableCell
tblMessageCell.Text = "File is well formed"
tblRow.Controls.Add(tblMessageCell)
tblFiles.Controls.Add(tblRow)
Catch ee As Exception
Dim tblRow As New TableRow
Dim tblFileCell As New TableCell
tblfilecell.BackColor = Color.LightGray
tblfilecell.Font.Bold = True
tblFileCell.Text = fileinfo.Name
tblRow.Controls.Add(tblFileCell)
Dim tblSchemaCell As New TableCell
tblSchemaCell.BackColor = Color.LightGray
tblSchemaCell.Font.Bold = True
tblSchemaCell.Text = internalschema.ToString
tblRow.Controls.Add(tblSchemaCell)
Dim tblMessageCell As New TableCell
tblmessagecell.ForeColor = Color.Red
tblMessageCell.Text = ee.Message
tblRow.Controls.Add(tblMessageCell)
tblFiles.Controls.Add(tblRow)
End Try
tr = Nothing
vr = Nothing
End If
fileinfo = Nothing
Next
file = Nothing
pnlFiles.Controls.Add(tblFiles)
pnlFiles.Visible = True
End If
Aside from the fact that it's pretty messy, it seems to be leaving all the files in use. I'm not sure why. Has anyone got any ideas what I'm not closing or what I'm doing wrong?
Thanks for any help.
Chris