Reading and Wrinting XML Files - Printable Version +- BP Forums (https://bpforums.info) +-- Forum: Archived Forums (https://bpforums.info/forumdisplay.php?fid=55) +--- Forum: Archived Forums (https://bpforums.info/forumdisplay.php?fid=56) +---- Forum: VB.NET (Visual Basic 2010/2008) (https://bpforums.info/forumdisplay.php?fid=8) +----- Forum: Code Snippets (https://bpforums.info/forumdisplay.php?fid=18) +----- Thread: Reading and Wrinting XML Files (/showthread.php?tid=700) |
Reading and Wrinting XML Files - Snake_eyes - 11-17-2012 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"?> 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 Re: Reading and Wrinting XML Files - brandonio21 - 11-17-2012 Thanks so much for writing this little guide! It will help me a lot. |