Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27531

.xml Reading / Writing

$
0
0
Hello all,

Not long started with VB so please bare with me.

Have been toying around with reading and writing from/to an xml file.

Individually both work fine, but when I read, and then try to write i get the following:

An unhandled exception of type 'System.IO.IOException' occurred in System.Xml.dll

Additional information: The process cannot access the file 'C:\Users\Andrew\Desktop\xmlread_write\xmlread_write\bin\Debug\Andrews.xml' because it is being used by another process.


I know there is something silly I am missing, terminating the read somehow? hopefully somebody can quickly tell me what i have been doing wrong.


Thanks in advance,
Andy

Code:

Imports System
Imports System.IO
Imports System.Xml

Public Class Form1

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

        Dim settings As New XmlWriterSettings()
        settings.Indent = True

        ' Initialize the XmlWriter.
        Dim XmlWrt As XmlWriter = XmlWriter.Create("Andrews.xml", settings)

        With XmlWrt

            ' Write the Xml declaration.
            .WriteStartDocument()

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

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

            ' Start our first person.
            .WriteStartElement("Person")

            ' The person nodes.

            .WriteStartElement("Name")
            .WriteString(txtName.Text.ToString())
            .WriteEndElement()

            .WriteStartElement("Email")
            .WriteString(txtEmail.Text.ToString())
            .WriteEndElement()

            .WriteStartElement("Tel")
            .WriteString(txtPhone.Text.ToString())
            .WriteEndElement()

            .WriteStartElement("Notes")
            .WriteString(txtNotes.Text.ToString())
            .WriteEndElement()

            ' The end of this person.
            .WriteEndElement()

            ' Close the XmlTextWriter.
            .WriteEndDocument()
            .Close()

        End With

        MessageBox.Show("XML file saved.")

        Exit Sub

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        If (TBxml.Text = "") Then

            MessageBox.Show("No file name entered")

        Else

            If (System.IO.File.Exists(TBxml.Text.ToString())) Then

                Dim document As XmlReader = New XmlTextReader(TBxml.Text.ToString())

                While (document.Read())

                    Dim type = document.NodeType

                    If (type = XmlNodeType.Element) Then

                        If (document.Name = "Name") Then

                            txtName.Visible = True
                            txtName.Text = document.ReadInnerXml.ToString()

                        End If

                        If (document.Name = "Email") Then

                            txtEmail.Visible = True
                            txtEmail.Text = document.ReadInnerXml.ToString()

                        End If

                        If (document.Name = "Tel") Then

                            txtPhone.Visible = True
                            txtPhone.Text = document.ReadInnerXml.ToString()

                        End If

                        If (document.Name = "Notes") Then

                            txtNotes.Visible = True
                            txtNotes.Text = document.ReadInnerXml.ToString()

                        End If

                    End If



                End While

                MessageBox.Show("File Read successfully")

            Else

                MessageBox.Show("The filename you selected was not found.")

            End If

        End If
    End Sub
End Class


Viewing all articles
Browse latest Browse all 27531

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>