Introduction
Today, in this article let's play around with one of the interesting and most useful concepts in XML in C#.
Question: What is create/pull/insert XML data out from scratch?
In simple terms "It provides flexibility to create a new XML structure, select the data from the created structure and insert new elements to that structure."
Step 1: Create a new "ASP.NET Web Application", as in:
Step 2: Add a new XML file item to the project and name it as Employee.xml.
Step 3: The complete code of Default.aspx looks like this:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="CreateXMLApp._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<center>
<div>
<table>
<tr>
<td colspan="2">
<asp:Label ID="Label1" runat="server" Text="Create/Insert/Pull XML Data Out from scratch"
Font-Bold="true" Font-Size="Large" Font-Names="Verdana" ForeColor="Maroon"></asp:Label>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<asp:Button ID="Button1" runat="server" Text="Create XML Data" Font-Names="Verdana"
Width="213px" BackColor="Orange" Font-Bold="True" OnClick="Button1_Click" Style="margin-top: 5%" />
</td>
</tr>
<tr>
<td colspan="2" align="center">
<asp:Button ID="Button2" runat="server" Text="Select Data" Font-Names="Verdana" Width="213px"
BackColor="Orange" Font-Bold="True" OnClick="Button2_Click" Style="margin-top: 5%" />
</td>
</tr>
<tr>
<td colspan="2" align="center">
<br />
<asp:GridView ID="GridView1" runat="server" BackColor="LightGoldenrodYellow" BorderColor="Tan"
BorderWidth="1px" CellPadding="2" EnableModelValidation="True" ForeColor="Black"
GridLines="None" AutoGenerateColumns="False">
<AlternatingRowStyle BackColor="PaleGoldenrod"></AlternatingRowStyle>
<FooterStyle BackColor="Tan"></FooterStyle>
<HeaderStyle BackColor="Tan" Font-Bold="True"></HeaderStyle>
<PagerStyle HorizontalAlign="Center" BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue">
</PagerStyle>
<SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite"></SelectedRowStyle>
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" ReadOnly="true" />
<asp:BoundField DataField="FirstName" HeaderText="First Name" />
<asp:BoundField DataField="LastName" HeaderText="Last Name" />
<asp:BoundField DataField="Age" HeaderText="Age" />
</Columns>
</asp:GridView>
</td>
<td colspan="3">
<fieldset>
<legend>Insert XML Data</legend>
<table>
<tr>
<td colspan="2" align="center">
<asp:Label ID="Label2" runat="server" Text="Insert XML Data" Font-Bold="true" Font-Size="Large"
Font-Names="Verdana" ForeColor="Maroon"></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label7" runat="server" Text="Please Enter Id" Font-Size="Large" Font-Names="Verdana"
Font-Italic="true"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label3" runat="server" Text="Please Enter FirstName" Font-Size="Large"
Font-Names="Verdana" Font-Italic="true"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label4" runat="server" Text="Please Enter LastName" Font-Size="Large"
Font-Names="Verdana" Font-Italic="true"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label6" runat="server" Text="Please Enter Age" Font-Size="Large" Font-Names="Verdana"
Font-Italic="true"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<asp:Button ID="Button3" runat="server" Text="Insert XML Data" Font-Names="Verdana"
Width="213px" BackColor="Orange" Font-Bold="True" OnClick="Button3_Click" />
</td>
</tr>
</table>
</fieldset>
</td>
</tr>
<tr>
<td colspan="4" align="center">
<asp:Label ID="Label5" runat="server" Font-Bold="true" Font-Names="Verdana"></asp:Label>
</td>
</tr>
</table>
</div>
</center>
</form>
</body>
</html>
Step 4: The complete code of Default.aspx.cs looks like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Xml.Linq;
namespace CreateXMLApp
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
XmlDocument xmlDoc = new XmlDocument();
XmlDeclaration dec = xmlDoc.CreateXmlDeclaration("1.0", null, null); xmlDoc.AppendChild(dec);
XmlElement rootEmployeesElement = xmlDoc.CreateElement("Employees");
xmlDoc.AppendChild(rootEmployeesElement);
XmlElement employeeElement = xmlDoc.CreateElement("Employee");
XmlElement idElement = xmlDoc.CreateElement("Id");
idElement.InnerText = "1"; XmlElement firstNameElement = xmlDoc.CreateElement("FirstName");
firstNameElement.InnerText = "Vijay"; XmlElement lastNameElement = xmlDoc.CreateElement("LastName");
lastNameElement.InnerText = "Prativadi";
XmlElement ageElement = xmlDoc.CreateElement("Age");
ageElement.InnerText = "26"; employeeElement.AppendChild(idElement);
employeeElement.AppendChild(firstNameElement); employeeElement.AppendChild(lastNameElement);
employeeElement.AppendChild(ageElement);
rootEmployeesElement.AppendChild(employeeElement);
xmlDoc.Save(@"c:\users\administrator\documents\visual studio 2010\Projects\CreateXMLApp\CreateXMLApp\Employee.xml");
Label5.Text = "XML Created Sucessfully"; Label5.ForeColor = System.Drawing.Color.Green;
}
protected void Button2_Click(object sender, EventArgs e)
{
XDocument document = XDocument.Load(@"C:\Users\Administrator\documents\visual studio 2010\Projects\CreateXMLApp\CreateXMLApp\Employee.xml");
var query = from r in document.Descendants("Employee") select new
{
Id = r.Element("Id").Value, LastName = r.Element("LastName").Value, FirstName = r.Element("FirstName").Value, Age = r.Element("Age").Value };
GridView1.DataSource = query;
GridView1.DataBind();
}
protected void Button3_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(TextBox4.Text) || string.IsNullOrEmpty(TextBox1.Text) || string.IsNullOrEmpty(TextBox2.Text) || string.IsNullOrEmpty(TextBox3.Text))
{
Label5.Text = "Please Enter Some Values";
Label5.ForeColor = System.Drawing.Color.Red;
}
else
{
XDocument document = XDocument.Load(Server.MapPath("Employee.xml"));
document.Element("Employees").Add(new XElement("Employee", new XElement("Id", TextBox4.Text), new XElement("FirstName", TextBox1.Text), new XElement("LastName", TextBox2.Text), new XElement("Age", TextBox3.Text)));
document.Save(Server.MapPath("Employee.xml")); Label5.Text = "Data Inserted Successfully";
Label5.ForeColor = System.Drawing.Color.Green;
TextBox4.Text = string.Empty;
TextBox1.Text = string.Empty;
TextBox2.Text = string.Empty;
TextBox3.Text = string.Empty;
}
}
}
}
Step 5: The output of the application looks like this:
Step 6: The output of creating new XML structure looks like this:
Step 7: The output of created new XML structure looks like this:
<?xml version="1.0"?>
<Employees>
<Employee>
<Id>1</Id>
<FirstName>Vijay</FirstName>
<LastName>Prativadi</LastName>
<Age>26</Age>
</Employee>
</Employees>
Step 8: The output of selecting data from XML structure looks like this:
Step 9: The output of inserting data to XML structure looks like this:
Step 10: The output of selecting data from XML structure looks like this:
Step 11: The output of inserted data to XML structure looks like this:
<?xml version="1.0" encoding="utf-8"?>
<Employees>
<Employee>
<Id>1</Id>
<FirstName>Vijay</FirstName>
<LastName>Prativadi</LastName>
<Age>26</Age>
</Employee>
<Employee>
<Id>2</Id>
<FirstName>Swetha</FirstName>
<LastName>Prativadi</LastName>
<Age>23</Age>
</Employee>
</Employees>
I hope this article is useful for you.