1

I am new to Visual Basic, and am creating a basic task manager program. I wanted to save my data in a xml file.

When it first loads it checks if the xml file exists and if not creates it

Private Sub frmTaskManager_Load(sender As Object, e As EventArgs) Handles Me.Load
    'Checks if tasks exists
    If IO.File.Exists("tasks.xml") Then
        GetSavedTasks()
    Else
        'Creates xml document
        CreateXMLDocument()
    End If
End Sub

Private Sub CreateXMLDocument()
        'If tasks don't exists then creates a new xml file
        Dim settings As New XmlWriterSettings()
        settings.Indent = True

        ' Initialize the XmlWriter.
        Dim XmlWrt As XmlWriter = XmlWriter.Create("Tasks.xml", settings)
        With XmlWrt
            ' Write the Xml declaration.
            .WriteStartDocument()

            ' Write a comment.
            .WriteComment("XML Database.")

            ' Write the root element.
            .WriteStartElement("Tasks")

            ' Close the XmlTextWriter.
            .WriteEndDocument()
            .Close()
        End With
    End Sub

The first time it creates it I can add to it using my save method after they enter a description of the task. Here is how I save.

Public Sub Save()
    Dim xmlDoc As XmlDocument = New XmlDocument()
    xmlDoc.Load("Tasks.xml")

    With xmlDoc.SelectSingleNode("Tasks").CreateNavigator.AppendChild
        .WriteStartElement("task")
        .WriteElementString("description", Description)
        .WriteEndElement()
        .Flush()
        .Close()
        .Dispose()
    End With
    xmlDoc.Save("Tasks.xml")
End Sub

Everything works fine the first time, when the xml document does NOT exist. The second time I start my project I get an error when I try to add to the xml file saying "An unhandled exception of type 'System.IO.IOException' occurred in mscorlib.dll

Additional information: The process cannot access the file 'C:\Users\Josh\SkyDrive\Projects\Visual Basic\Task Manager\Task Manager\bin\Debug\Tasks.xml' because it is being used by another process."

Any ideas what could be causing this? Also is XML a bad choice to store data in for a VB project?

1

1 Answer 1

2

You are not disposing the XmlWriter (it is not enough just with closing it) and that's why the second time an error is triggered (the object is still "alive"). Using takes care of everything (disposing and closing):

 Using XmlWrt As XmlWriter = XmlWriter.Create("Tasks.xml", settings)
     With XmlWrt
         ' Write the Xml declaration.
         .WriteStartDocument()

         ' Write a comment.
         .WriteComment("XML Database.")

         ' Write the root element.
         .WriteStartElement("Tasks")

         ' Close the XmlTextWriter.
         .WriteEndDocument()
     End With
 End Using
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.