
Navigator : Home > Tutorials > XML Tutorials > ...
Adding to XML File using WinForms and VB
This tutorial will show how we can add data to an XML file using WinForms. VB version.
WinForms are the Windows Applications we can create in Visual Studio. In this tutorial, we will explore how we can use LINQ to add data to an XML form, and then how we can extract this data and display it on our form.
The first thing that we will do is design our form. We will have three textboxes, two buttons and a Rich Text Box. We will use the textboxes to input data to be added to the XML file, and then one button to submit the data to be added and the other button to display the XML file in the RichTextBox.
The form will look something like this:
Once we have our form designed, we can start programming the buttons. On the first button, we will need to create an XML document with the data provided in the textboxes. We will use the following code:
We migrated our web sites to Server Intellect over one weekend and the setup was so smooth that we were up and running right away. They assisted us with everything we needed to do for all of our applications. With Server Intellect's help, we were able to avoid any headaches!
| Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim xml As New XElement("Persons", New XElement("Person", New XElement("Name", txtName.Text), New XElement("City", txtCity.Text), New XElement("Age", txtAge.Text)))
xml.Save("XMLFile.xml") End Sub |
This block of code creates a new XML structure for the textbox data. In this example, we have Name, City and Age. The XML output of this code will look something like this:
<?xml version="1.0" encoding="utf-8"?> <Persons>
<Person>
<Name>Mike</Name> <City>Orlando</City> <Age>33</Age> </Person> </Persons> |
The other button will display the XML file once we have added to it. For this button, we can use the following code:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim xmlDoc As XDocument = XDocument.Load("XMLFile.xml") Dim persons = From person In xmlDoc.Descendants("Person") _
Select Name = person.Element("Name").Value, City = person.Element("City").Value, _ Age = person.Element("Age").Value RichTextBox1.Text = "" For Each person In persons
RichTextBox1.Text = RichTextBox1.Text & "Name: " & person.Name + Constants.vbLf RichTextBox1.Text = RichTextBox1.Text & "City: " & person.City + Constants.vbLf RichTextBox1.Text = RichTextBox1.Text & "Age: " & person.Age + Constants.vbLf + Constants.vbLf Next person If RichTextBox1.Text = "" Then
RichTextBox1.Text = "No Results." End If End Sub |
This method loads the XML file and then checks for the Name, City and Age elements. It returns all the data in the XML file and outputs it to the RichTextBox. The entire code-behind for this application is as follows:
Server Intellect offers Windows Hosting Dedicated Servers at affordable prices. I'm very pleased!
Imports System.Xml Imports System.Xml.Linq
Public Class Form1
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim xmlDoc As XDocument = XDocument.Load("XMLFile.xml") Dim persons = From person In xmlDoc.Descendants("Person") _
Select Name = person.Element("Name").Value, City = person.Element("City").Value, _ Age = person.Element("Age").Value RichTextBox1.Text = "" For Each person In persons
RichTextBox1.Text = RichTextBox1.Text & "Name: " & person.Name + Constants.vbLf RichTextBox1.Text = RichTextBox1.Text & "City: " & person.City + Constants.vbLf RichTextBox1.Text = RichTextBox1.Text & "Age: " & person.Age + Constants.vbLf + Constants.vbLf Next person If RichTextBox1.Text = "" Then
RichTextBox1.Text = "No Results." End If End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim xml As New XElement("Persons", New XElement("Person", New XElement("Name", txtName.Text), New XElement("City", txtCity.Text), New XElement("Age", txtAge.Text)))
xml.Save("XMLFile.xml") End Sub End Class |
Looking for the C# 2005 Version? Click Here!
Looking for more .NET Tutorials? Click Here!