Adding to XML File using LINQ and C#

This tutorial was created with Visual Studio 2008, but can be replicated in 2005 if you download and install Microsoft's LINQ Community Technology Preview release, which can be downloaded from here.
LINQ has the amazing ability to communicate and interact with many various data sources - databases, XML and varying objects. In this tutorial, we will be using a Windows Form to look at how we can use LINQ to add data to an XML file for storage. This is a quick and easy alternative to using a database. An XML file would be good for storing less information, which is not sensitive.

The XML file we will be using in this example is as follows:



We will create a Windows Form to view the XML file, and also to add new data to it. We will add three textboxes to the form - for name, city and age; two buttons - for adding and viewing XML data; and a Rich Text Box for viewing the XML data. We can also add labels to the text boxes.
Once our form is ready, we can move to the code-behind and add the namespaces we are going to use. The majority of which are added automatically. We may need to add System.Xml.Linq, however. It will look something like this:
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;
Once we have added our references, we will create an on-click handler for the view button. We will add the following code-behind:
private void button1_Click(object sender, EventArgs e){
XDocument xmlDoc = XDocument.Load("People.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,
};
txtResults.Text = "";
foreach (var person in persons){
txtResults.Text = txtResults.Text + "Name: " + person.Name + "\n";

txtResults.Text = txtResults.Text + "City: " + person.City + "\n";
txtResults.Text = txtResults.Text + "Age: " + person.Age + "\n\n";
}
if (txtResults.Text == "")
txtResults.Text = "No Results.";

}
This code-block utilizes LINQ, first loading the XML file into memory, and then selecting all the data from within. The LINQ query looks similar to a SQL statement. We select all data from the XML file and then loop through this data, outputting all results into the text box. Finally, we check to see if the XML file is empty.
Next, we will add the code for the button to add new data to the XML file. We create another handler for the other button:
private void button2_Click(object sender, EventArgs e){
XDocument xmlDoc = XDocument.Load("People.xml");

xmlDoc.Element("Persons").Add(new XElement("Person", new XElement("Name", txtName.Text),
new XElement("City", txtCity.Text), new XElement("Age", txtAge.Text)));

xmlDoc.Save("People.xml");
}
Similar to the other button, this one also opens the XML file, to enable us to edit it. We then simply add an element to the XML file, into the Persons element.Now our Window Form can both read and write to the XML file. The entire code-behind looks something like this:
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 LINQtoXML_cs{
public partial class Form1 : Form{
public Form1(){
InitializeComponent();

}
private void button1_Click(object sender, EventArgs e){
XDocument xmlDoc = XDocument.Load("People.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,
};
txtResults.Text = "";
foreach (var person in persons){
txtResults.Text = txtResults.Text + "Name: " + person.Name + "\n";

txtResults.Text = txtResults.Text + "City: " + person.City + "\n";
txtResults.Text = txtResults.Text + "Age: " + person.Age + "\n\n";
}
if (txtResults.Text == "")
txtResults.Text = "No Results.";

}
private void button2_Click(object sender, EventArgs e)
{
XDocument xmlDoc = XDocument.Load("People.xml");xmlDoc.Element("Persons").Add(new XElement("Person", new XElement("Name", txtName.Text),
new XElement("City", txtCity.Text), new XElement("Age", txtAge.Text)));xmlDoc.Save("People.xml");

}
}
}

0 Responses to "Adding to XML File using LINQ and C#"