SIGN UP MEMBER LOGIN:    
ARTICLE

Manipulating XML file with C#

Posted by Rajshree Mittal Articles | XML in C# September 15, 2010
In this article you will learn how to Manipule XML file with C#.
Reader Level:
Download Files:
 

XMLRead.aspx.cs

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.IO;

 

public partial class XMLRead : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

        if (!Page.IsPostBack)

        {

            getContent();

        }

 

 

    }

    protected void btnsearch_Click(object sender, EventArgs e)

    {

 

        btnup.Visible = true;

        btndel.Visible = true;

        FileStream fs = new FileStream(@"C:\Documents and Settings\rajshree\Desktop\Library.xml", FileMode.Open, FileAccess.ReadWrite);

        XmlDocument doc1 = new XmlDocument();

        doc1.Load(fs);

        XmlNodeList nodeList = doc1.SelectNodes("/catalog/book[@id='" + ddBookId.SelectedItem.Text + "']");

        if (nodeList.Count > 0)

        {

            txtid.Text = ddBookId.SelectedItem.Text;

            txtauthor.Text = nodeList[0].ChildNodes[0].InnerText;

            txttitle.Text = nodeList[0].ChildNodes[1].InnerText;

            txtgen.Text = nodeList[0].ChildNodes[2].InnerText;

            txtprice.Text = nodeList[0].ChildNodes[3].InnerText;

            txtpub.Text = nodeList[0].ChildNodes[4].InnerText;

            txtdesc.Text = nodeList[0].ChildNodes[5].InnerText;

        }

 

        fs.Close();

 

    }

    protected void getContent()

    {

 

        //XmlTextReader xr=new XmlTextReader(@"C:\Documents and Settings\rajshree\Desktop\Library.xml");

        //while (xr.Read())

        //{

        //   if (xr.HasAttributes)

        //    {

        //          for (int i = 0; i < xr.AttributeCount; i++)

        //        {

 

        //            ddBookId.Items.Add(xr[i]);

        //        }

 

        //        ddBookId.DataBind();

        //    }

        //}

        //xr.Close();

 

        XmlDocument doc = new XmlDocument();

        doc.Load(@"C:\Documents and Settings\rajshree\Desktop\Library.xml");

        XmlNodeList nodeList = doc.SelectNodes("/catalog/book");

 

        foreach (XmlElement book in doc.SelectNodes(@"catalog/book"))

        {

            ddBookId.Items.Add(book.GetAttribute("id"));

        }

 

    }

 

    protected void btnadd_Click(object sender, EventArgs e)

    {

 

 

        XmlDocument doc = new XmlDocument();

 

        doc.Load(@"C:\Documents and Settings\rajshree\Desktop\Library.xml");

 

        XmlElement root = doc.CreateElement("book");

        root.SetAttribute("id", txtid.Text);

        XmlElement title = doc.CreateElement("title");

        XmlElement author = doc.CreateElement("author");

        XmlElement genre = doc.CreateElement("genre");

        XmlElement price = doc.CreateElement("price");

        XmlElement publish_date = doc.CreateElement("publish_date");

        XmlElement description = doc.CreateElement("description");

 

        title.InnerText = txttitle.Text;

        author.InnerText = txtauthor.Text;

        genre.InnerText = txtgen.Text;

        price.InnerText = txtprice.Text;

        publish_date.InnerText = txtpub.Text;

        description.InnerText = txtdesc.Text; ;

 

        root.AppendChild(author);

        root.AppendChild(title);

        root.AppendChild(genre);

        root.AppendChild(price);

        root.AppendChild(publish_date);

        root.AppendChild(description);

 

        doc.DocumentElement.AppendChild(root);

        doc.Save(@"C:\Documents and Settings\rajshree\Desktop\Library.xml");

 

        lblerror.Text = "Element has been added successfully";

        lblerror.Visible = true;

        txtauthor.Text = "";

        txttitle.Text = "";

        txtdesc.Text = "";

        txtgen.Text = "";

        txtid.Text = "";

        txtprice.Text = "";

        txtpub.Text = "";

    }

 

    protected void btnup_Click(object sender, EventArgs e)

    {

 

        XmlDocument doc1 = new XmlDocument();

        doc1.Load(@"C:\Documents and Settings\rajshree\Desktop\Library.xml");

        XmlNodeList nodeList = doc1.SelectNodes("/catalog/book[@id='" + ddBookId.SelectedItem.Text + "']");

        nodeList[0].ChildNodes[4].InnerText = txtpub.Text;

 

        doc1.Save(@"C:\Documents and Settings\rajshree\Desktop\Library.xml");

        lblerror.Visible = true;

        lblerror.Text = "Updated Successfully";

        txtauthor.Text = "";

        txttitle.Text = "";

        txtdesc.Text = "";

        txtgen.Text = "";

        txtid.Text = "";

        txtprice.Text = "";

        txtpub.Text = "";

 

 

    }

    protected void btndel_Click(object sender, EventArgs e)

    {

        XmlDocument doc2 = new XmlDocument();

        doc2.Load(@"C:\Documents and Settings\rajshree\Desktop\Library.xml");

        XmlNode nodeList = doc2.SelectSingleNode("/catalog/book[@id='" + ddBookId.SelectedItem.Text + "']");

        doc2.DocumentElement.RemoveChild(nodeList);

        doc2.Save(@"C:\Documents and Settings\rajshree\Desktop\Library.xml");

        lblerror.Text = "Deleted Successfully";

        lblerror.Visible = true;

        txtauthor.Text = "";

        txttitle.Text = "";

        txtdesc.Text = "";

        txtgen.Text = "";

        txtid.Text = "";

        txtprice.Text = "";

        txtpub.Text = ""
 
    }
}

ExWithGrid.aspx.cs
 

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.Data;

 

public partial class ExWithGrid : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

        if (!Page.IsPostBack)

        {

            getdata();

        }

    }

    private void getdata()

    {

        DataSet ds = new DataSet();

        ds.ReadXml(@"C:\Documents and Settings\rajshree\Desktop\Library.xml");

        GridView1.DataSource = ds;

        GridView1.DataBind();

    }

 

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)

    {

        GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];

        string id = row.Cells[1].Text.Trim();

 

        XmlDocument doc2 = new XmlDocument();

        doc2.Load(@"C:\Documents and Settings\rajshree\Desktop\Library.xml");

        XmlNode nodeList = doc2.SelectSingleNode("/catalog/book[@id='" + id + "']");

        doc2.DocumentElement.RemoveChild(nodeList);

        doc2.Save(@"C:\Documents and Settings\rajshree\Desktop\Library.xml");

        getdata();

    }

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)

    {

        GridView1.EditIndex = e.NewEditIndex;

        getdata();

    }

 

    protected void GridView1_RowUpdated(object sender, GridViewUpdatedEventArgs e)

    {

        getdata();

    }

    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)

    {

        GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];

        string pub = row.Cells[6].Text.Trim();

        XmlDocument doc1 = new XmlDocument();

        doc1.Load(@"C:\Documents and Settings\rajshree\Desktop\Library.xml");

        XmlNodeList nodeList = doc1.SelectNodes("/catalog/book[@id='" + row.Cells[1].Text + "']");

        nodeList[0].ChildNodes[4].InnerText = pub;

 

        doc1.Save(@"C:\Documents and Settings\rajshree\Desktop\Library.xml");

        getdata();

    }

    protected void GridView1_RowUpdating1(object sender, GridViewUpdateEventArgs e)

    {

        GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];

        string pub = (row.Cells[6].Controls[0] as TextBox).Text;

        string a = row.Cells[2].Text;

        XmlDocument doc1 = new XmlDocument();

        doc1.Load(@"C:\Documents and Settings\rajshree\Desktop\Library.xml");

        XmlNodeList nodeList = doc1.SelectNodes("/catalog/book[@id='" + row.Cells[1].Text + "']");

        nodeList[0].ChildNodes[4].InnerText = pub;

        doc1.Save(@"C:\Documents and Settings\rajshree\Desktop\Library.xml");

        getdata();

    }

    protected void GridView1_RowCancelingEdit1(object sender, GridViewCancelEditEventArgs e)

    {

        GridView1.EditIndex = -1;

        getdata();

    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)

    {

        GridView1.PageIndex = e.NewPageIndex;

        getdata();

 

    }

}

Login to add your contents and source code to this article
share this article :
post comment
 

<?xml version="1.0" ?> - <Books> - <book id="hello"> <author>hello</author> <title>hello</title> <genre>hello</genre> <price>20</price> <publish_date>01/01/2011</publish_date> <description>hello</description> </book> </Books>

Posted by tunisian Dec 22, 2010

Hi can you sent me the Library xml file..its missing in zip file.
thanks

Posted by rubul kumar Nov 07, 2010

Hi Rajshree,

Library.xml is missing from solution. Can you upload solution with xml file?

Thanks,
Vlad

Posted by Vladimir Dimitrijevic Sep 22, 2010

nice.

Posted by vivek dwivedi Sep 17, 2010
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Nevron Gauge for SharePoint
Become a Sponsor