XML Parameters in a form

hobbit666

Member
Joined
Jun 7, 2007
Messages
6
Location
Newtown, Wales
Programming Experience
Beginner
OK i admit this is doing my nut in, and i need help as i can't find where my code is going wrong.

I have a form that loads 3 parameters from an XML file and puts them into text boxes. This works fine. But if i modify the text feild(s) and click save it should save the changes to the XML file. But that's where it fails it keeps comming up with the file is already in use.

I'm gone through the example that i have been following up still can't get it working.

Here is the XML Doc

VB.NET:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Webmonitorparameters>
	<FTPAddress>ftp.*********.co.uk</FTPAddress>
	<FTPUsername>**********</FTPUsername>
	<FTPPassword>********</FTPPassword>>
</Webmonitorparameters>and here is the VB

Here's the VB

VB.NET:
Imports System.Xml

Public Class Settings

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

        Me.Close()

    End Sub

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

        Dim Doc As New XmlDocument

        'create a XML declaration
        Dim myXmlDeclaration As XmlDeclaration
        myXmlDeclaration = Doc.CreateXmlDeclaration("1.0", "UTF-8", "yes")

        'create the XML Root
        Dim Root As XmlElement
        Root = Doc.CreateElement("Webmonitorparameters")

        'create the XML Childs
        Dim Child As XmlElement
        Child = Doc.CreateElement("FTPAddress")
        Child.InnerText = txtFTPAddress.Text
        Root.AppendChild(Child)

        Child = Doc.CreateElement("FTPUsername")
        Child.InnerText = txtFTPusername.Text
        Root.AppendChild(Child)

        Child = Doc.CreateElement("FTPPassword")
        Child.InnerText = txtFTPPassword.Text
        Root.AppendChild(Child)

        Doc.AppendChild(Root)
        
        'Write XML to the File
        Dim Output As New XmlTextWriter("WebMonitorSettings.xml", System.Text.Encoding.UTF8)

        Doc.WriteTo(Output)
        Output.Close()

    End Sub

    Private Sub Settings_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim Doc As New XmlDocument
        Dim Input As New XmlTextReader("WebMonitorSettings.xml")
        Input.WhitespaceHandling = WhitespaceHandling.None
        Do While Input.Read()
            If Input.NodeType = XmlNodeType.Element Then
                Select Case Input.Name
                    Case "FTPAddress"
                        txtFTPAddress.Text = Input.ReadString()
                    Case "FTPUsername"
                        txtFTPusername.Text = Input.ReadString()
                    Case "FTPPassword"
                        txtFTPPassword.Text = Input.ReadString()
                End Select
            End If
        Loop
    End Sub
End Class
Right so "Button1" is my Save button, "button3" is a close button and "settings" is my Form name. I am going to start a new project and try this again but hay i thought i would ask for a bit of help while i work on it.
 
Last edited:
It sounds as you have opened this xml file for reading and not closed it before attempting to overwrite it.
 
Some more info: If you have a XmlDocument open it is usually easier to just set some nodes values there and save that, rather than writing a new document. If you want to write a new document anyway it is also usually easier to use the XmlTextWriter than the XmlDocument class.
 
Back
Top