Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XML validating is leaving files locked

Status
Not open for further replies.

cmhunt

Programmer
Apr 17, 2001
119
GB
Hi

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
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
 
It seems you are setting vr and tr wo nothing and not closing the connection to the file, try

vr.Close()
tr.Close()
 
Brilliant, cheers. Misunderstanding on my behalf.

Thanks a lot

Chris
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top