DotNet Tutorials

Server Intellect

 Adding to XML File using WinForms and C#

This tutorial will show how we can add data to an XML file using WinForms. C# 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:

Server Intellect offers Windows Hosting Dedicated Servers at affordable prices. I'm very pleased!

private void button1_Click(object sender, EventArgs e)
{
XElement xml = 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");
}

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 void button2_Click(object sender, EventArgs e)
{
XDocument xmlDoc = XDocument.Load("XMLFile.xml");

var persons = from person in xmlDoc.Descendants("Person")
select new
{
Name = person.Element("Name").Value,
City = person.Element("City").Value,
Age = person.Element("Age").Value,
};

richTextBox1.Text = "";
foreach (var person in persons)
{
richTextBox1.Text = richTextBox1.Text + "Name: " + person.Name + "\n";
richTextBox1.Text = richTextBox1.Text + "City: " + person.City + "\n";
richTextBox1.Text = richTextBox1.Text + "Age: " + person.Age + "\n\n";
}

if (richTextBox1.Text == "")
richTextBox1.Text = "No Results.";
}

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:

We are using Server Intellect and have found that by far, they are the most friendly, responsive, and knowledgeable support team we've ever dealt with!

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;

namespace WinForm_XML_Adding_cs
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button2_Click(object sender, EventArgs e)
{
XDocument xmlDoc = XDocument.Load("XMLFile.xml");

var persons = from person in xmlDoc.Descendants("Person")
select new
{
Name = person.Element("Name").Value,
City = person.Element("City").Value,
Age = person.Element("Age").Value,
};

richTextBox1.Text = "";
foreach (var person in persons)
{
richTextBox1.Text = richTextBox1.Text + "Name: " + person.Name + "\n";
richTextBox1.Text = richTextBox1.Text + "City: " + person.City + "\n";
richTextBox1.Text = richTextBox1.Text + "Age: " + person.Age + "\n\n";
}

if (richTextBox1.Text == "")
richTextBox1.Text = "No Results.";
}

private void button1_Click(object sender, EventArgs e)
{
XElement xml = 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");
}
} }

Looking for the VB 2005 Version? Click Here!

Looking for more .NET Tutorials? Click Here!

Download Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!



100% SPAM FREE! We will never sell or rent your email address!
 
123 ASP

411 ASP

Dot Net Freaks

Server Intellect