Thursday, August 21, 2014

Linq To Xml

 
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="UploadImages.WebForm1" %>

<!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 runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <strong>Add To XML</strong><br />
    Name:<br />
    <asp:TextBox ID="txtName" runat="server" /><br />
    City:<br />
    <asp:TextBox ID="txtCity" runat="server" />
    <br />
    Age:<br />
    <asp:TextBox ID="txtAge" runat="server" />
    <br />
    <asp:Button ID="butAdd" runat="server" Text="Add" onclick="butAdd_Click" /><br />
    <asp:Label ID="lblStatus" runat="server" /><br />
    <br />
    <strong>Read XML:</strong><br />
    <asp:Button ID="butRead" runat="server" Text="Read" onclick="butRead_Click" /><br />
    <asp:Literal ID="litResults" runat="server" />
    </form>
</body>
</html>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Linq;

namespace UploadImages
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void butRead_Click(object sender, EventArgs e)
        {
            readXML();
            lblStatus.Text = "";

        }

        protected void butAdd_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtName.Text == "" || txtCity.Text == "" || txtAge.Text == "")
                    lblStatus.Text = "Please complete the form.";
                else
                {
                    XDocument xmlDoc = XDocument.Load(Server.MapPath("XMLFile1.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(Server.MapPath("XMLFile1.xml"));
                    lblStatus.Text = "Data successfully added to XML file.";
                    readXML();
                }
            }
            catch
            {
                lblStatus.Text = "Sorry, unable to process request. Please try again.";
            }
        }
        protected void readXML()
        {
            XDocument xmlDoc = XDocument.Load(Server.MapPath("XMLFile1.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,
                          };

            litResults.Text = "";
            foreach (var person in persons)
            {
                litResults.Text = litResults.Text + "Name: " + person.Name + "<br />";
                litResults.Text = litResults.Text + "City: " + person.City + "<br />";
                litResults.Text = litResults.Text + "Age: " + person.Age + "<br /><br />";
            }

            if (litResults.Text == "")
                litResults.Text = "No Results.";
        }
    }
}
<?xml version="1.0" encoding="utf-8"?>
<Persons>
    <Person>
    <Name>naresh</Name>
    <City>Hyd</City>
    <Age>22</Age>
  </Person>
    <Person>
    <Name>naresh</Name>
    <City>wgl</City>
    <Age>22</Age>
  </Person>
</Persons>