Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Reading and Wrinting XML Files
#1
Here is the most simple way to read and write an xml file with attributes and values.

For the sake of this example let's say we have an application that on form load creates toolstrip butons that when cliked load a web page on a web browser

First of all let's see the format of the Xml file
Code:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<root>
   <ToolStipButtons>
      <item Name="btn1" url="www.google.com" btnName ="Google" />
      <item name="btn2" url="www.bing.com" btnText="Bing" />
      <item name="btn2" url="www.yahoo.com" btnText="Yahoo" />
   </ToolStripButtons>  
</root>

In order for the code to work we need to import System.Xml
[code2=vbnet]Imports System.Xml[/code2]

Reading the xml file

Now let's see about reading the values from the xml file and creating the buttons.
[code2=vbnet]Public Sub CreateButons()
Dim Xml_Doc As New XmlDocument
Xml_Doc.Load(Application.StartupPath & "/AppSettings.xml")
Dim xmlNode As XmlNode
For Each xmlNode In xmlDoc.SelectNodes("/root/ToolStripButtons/item")
Dim NewBtn As New ToolStripButton With {.Name = node.Attributes("name").Value.ToString, _
.Tag = node.Attributes("url").Value.ToString, _
.Text = node.Attributes("btnText").Value.ToString} }
AddHandler NewBtn.Click, AddressOf NewBtn_Click
ToolStrip1.Items.Add(NewBtn)
Next
End Sub[/code2]

After this just create a sub named "NewBtn_Click" to handle the click event of the individual buttons

Writing the xml file

If changes occured to the ToolStripButtons and we need to save the new values to the xml file then we will use the folowing code:

[code2=vbnet]Public Sub SaveValues()

Dim settings As New XmlWriterSettings
settings.Indent = True
settings.NewLineOnAttributes = False

Dim Xml_doc As New XmlDocument
Xml_doc.Load(Application.StartupPath & "\AppSettings.xml")
Xml_doc.RemoveAll()

Dim Writer As XmlWriter = XmlWriter.Create(Application.StartupPath & "\AppSettings.xml", settings)

With Writer
.WriteStartDocument(True)
.WriteStartElement("root")
.WriteStartElement("ToolStripButtons")

For Each Btn As ToolStripButton In ToolStrip1.Items
'//Writes the Item Element
.WriteStartElement("item")
'// Writes the "Name" atribute and it's value
.WriteStartAttribute("name")
.WriteValue(Btn.Name)
.WriteEndAttribute()
'// Writes the "url" atribute and it's value
.WriteStartAttribute("url")
.WriteValue(Btn.Tag)
.WriteEndAttribute()
'// Writes the "btnText" atribute and it's value
.WriteStartAttribute("btnText")
.WriteValue(Btn.Text)
.WriteEndAttribute()
'// Writes the end of the item element
.WriteEndElement()

Next
.WriteEndElement()
.WriteEndElement()
.WriteEndDocument()
.Flush()
.Close()
End With

End Sub[/code2]


Now just simply call the CreateButons() on the Form_Load event and the SaveValues() in the Form_Closing event(or any other sub for that mater, depending on your needs).

I also recommend using Try/Catch blocks in order to catch any unwanted errors that might occur
Sssssssssoftware developer...
#2
Thanks so much for writing this little guide! It will help me a lot.
My Blog | My Setup | My Videos | Have a wonderful day.


Possibly Related Threads…
Thread Author Replies Views Last Post
  Saving and Reading oAuth Tokens with TwitterVB brandonio21 6 20,894 08-14-2011, 09:47 PM
Last Post: brandonio21

Forum Jump:


Users browsing this thread: 1 Guest(s)