Post: [VB.NET] Saving to an XML File Class
02-16-2011, 08:12 PM #1
Default Avatar
Oneup
Guest
(adsbygoogle = window.adsbygoogle || []).push({}); Currently I've been working on a program and I needed to save settings and I didn't wanna use the registry because I don't quiet know the permissions on the machine this program will be running on. So I did some reading on XML and found it to be quiet simple to write out to an xml file.


Start a new class
I called mine settingsClass


Since we are using XML we need to use this import:
    Imports System.Xml


Now there are a few ways to do this, I haven't gotten around to reading the XML file back in to load the settings so you could have writeonly properties but since I know I will be reading from them as well I left them the way they are. You could name the properties whatever you need, just for my project I called them IP and port.

Declare local Variables:
        Private _IPaddressString As String
Private _PortNumberString As String


Write the Properties that will receive input and give back to the whatever calls it.

        Property setIPAddress() As String

Get
Return _IPaddressString
End Get
Set(ByVal value As String)
_IPaddressString = value
Call SaveSettings()
End Set
End Property

Property setPort() As String
Get
Return _PortNumberString
End Get
Set(ByVal value As String)
_PortNumberString = value
End Set
End Property


Now I put the call in the IP address since that's the first property that gets written (it's not really the correct way to do it, you should call the method from the program and pass the arguments that way, but this works just as well.)

Method
        Public Sub SaveSettings()

Dim SaveWriter As XmlWriter = XmlWriter.Create("Settings.xml")
SaveWriter.WriteStartElement("Settings")
SaveWriter.WriteElementString("IPADDRESS", _IPaddressString)
SaveWriter.WriteEndElement()
SaveWriter.Flush()

End Sub


This sets up the xml file and saves it to where the program is currently located. _IPaddress is the variable we used in the property which is set in the program.

To use this class, on your form you need to instantiate the class and make a Module level variable.

    Dim ixmlsettings As settingsClass


Now for the button click event we are going to make it a new instance of the settingsClass

        Private Sub btn_Save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Save.Click
Dim Ipaddress As String
Ipaddress = txt_IPAddress.Text.Replace(" ", "")

ixmlsettings = New settingsClass
ixmlsettings.setIPAddress = txt_IPAddress.Text


End Sub


I used a masked textbox to give a format for the user to enter so the txt_IPAddress.Text.Replace is just removing the spaces.

Originally posted by another user
ixmlsettings.setIPAddress = txt_IPAddress.Text

Is setting the setIPAddress property which will then fire the method to write out to the file.

So for my program it looks a little something like this:
You must login or register to view this content.

The outcome being:
You must login or register to view this content.
02-17-2011, 11:14 PM #2
Default Avatar
Oneup
Guest
Originally posted by Ownage
Very nice tut. I learned about xml's a couple of years ago in school


It was something I was getting around to learning but I never did till someone wanted me to do some work for them and their current class for parsing XML files was really long and a little confusing
03-03-2011, 03:00 AM #3
nice i learned this along time ago

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo