Home > Linq, XML > LINQ to XML to Add and Read Data from XML File

LINQ to XML to Add and Read Data from XML File


Hi

try this example to use LINQ to XML to Add  and Read Data from XML File

1) Open VS 2008 and create new website

2) Add new XML file  called :- Pepole.xml

<?xml version="1.0" encoding="utf-8"?>
<Persons>
  <Person>
    <Name>Yasser</Name>
    <City>Cairo</City>
    <Age>25</Age>
  </Person>
</Persons>

3) Add new web page :-

<div>
    <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" />
    </div>

and in code behind :-

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

    private void readXML()
    {
        XDocument xDoc = XDocument.Load(Server.MapPath("~/People.xml"));
        var persons = from person in xDoc.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 />";

        }
    }

    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("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(Server.MapPath("People.xml"));
                lblStatus.Text = "Data successfully added to XML file.";
                readXML();
            }
        }
        catch
        {
            lblStatus.Text = "Sorry, unable to process request. Please try again.";
        }
    }

Hope this helps

Good Luck

Advertisements
Categories: Linq, XML
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: