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 Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XMLReader keeps file open...

Status
Not open for further replies.

CharlieIT

MIS
Joined
Apr 9, 2003
Messages
157
Location
US
I am using the code below to open and read an xml file which is located on a remote computer. It does this fine, but once the code is executed, the XML file on the remote machine stays "open"--I cannot delete it, rename it, etc. because of a sharing violation.

I have executed "reader.Close" and "Stream.Close", but they do not close the file.

Any Ideas?

Dim Time As String

Try
' open the document...

Dim filestream As New System.IO.FileStream("\\" &_ machineName & "\" & "d$\Dat.xml", IO.FileMode.Open)

Dim reader As New XmlTextReader(filestream)

' move to the start of the document...
reader.MoveToContent()

' ignore whitespaces
reader.WhitespaceHandling = WhitespaceHandling.None

' start working through the document...
Do While reader.Read

' what kind of node is it?
Select Case reader.NodeType

' is it the start of an element?
Case XmlNodeType.Element

' if it's an element start, is it "logontime"?
If reader.Name = "logontime" Then

'If so, then read the value of the element
reader.ReadStartElement()
Time = reader.Value
End If

End Select
Loop

filestream.Close()
reader.Close()
Return Time

Catch ex As Exception
Time = ex.Message
End Try


End Function
 
Are you calling Dispose on your objects?

Objects that deal with files are using unmanaged resources, and so you have to wait for the garbage collector to run. By calling Dispose (on objects that implement IDisposable) you don't have this delay.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top