Wednesday, 30 November 2016

How to read, select and update xml file in asp.net c#

Suppose we have  PopUp.xml file stored in Add_Data Folder and has following structure

<?xml version="1.0" encoding="utf-8"?>
<PopUp>
  <ProjectId>Zgv1wTgdGV</ProjectId>
  <ProjectName>Shree Siddhivinayak Green County</ProjectName>
  <Address>Yawat</Address>
  <ImageName>imgHomePopUp.jpg</ImageName>
  <InsertedBy>preetam@ranjeetdevelopers.com</InsertedBy>
  <InsertedOn>2016-10-24: 17:12:23.469</InsertedOn>
</PopUp>

to read and bind data to a dataset

 XmlTextReader xmlreader = new XmlTextReader(Server.MapPath("~/App_Data/PopUp.xml"));
            DataSet ds = new DataSet();
            ds.ReadXml(xmlreader);
            xmlreader.Close();

            if (ds.Tables.Count != 0)
            {
                RepeaterPopUp.DataSource = ds;
                RepeaterPopUp.DataBind();
            }


To Read and Update

XmlTextReader xmlreader = new XmlTextReader(Server.MapPath("~/App_Data/PopUp.xml"));
            DataSet ds = new DataSet();
            ds.ReadXml(xmlreader);
            xmlreader.Close();
            if (ds.Tables.Count != 0)
            {
               
                XmlDocument xDoc = new XmlDocument();
                string xmlFile = System.Web.HttpContext.Current.Server.MapPath("~/App_Data/PopUp.xml");
                xDoc.Load(xmlFile);
                        //To Read Specific Node
                        //string nodeValue = xDoc.DocumentElement.SelectSingleNode("ProjectId").InnerText;
                        //Response.Write(nodeValue);

                StringBuilder ImageName = new StringBuilder();
                FileInfo FileInf = new FileInfo(FileUpload1.PostedFile.FileName);
                string ext = FileInf.Extension;
                ImageName.Append("imgHomePopUp");
                ImageName.Append(ext);
                //ImageName.Append(FileUpload1.PostedFile.FileName);
                string FImageName = ImageName.ToString();
                string path = "~/images/home-banner/" FImageName;             
                FileUpload1.PostedFile.SaveAs(MapPath(path));

                clsProject objProject = new clsProject();

                string projectId = ddlProject.SelectedValue;
                DataTable dt = objProject.Edit(projectId);
                string projectName = Convert.ToString(dt.Rows[0]["Title"]);
                string address = Convert.ToString(dt.Rows[0]["Address"]);

                 //To Read Specific Node
                xDoc.DocumentElement.SelectSingleNode("ProjectId").InnerText = projectId;
                xDoc.DocumentElement.SelectSingleNode("ProjectName").InnerText = projectName;
                xDoc.DocumentElement.SelectSingleNode("Address").InnerText = address;
                xDoc.DocumentElement.SelectSingleNode("ImageName").InnerText = FImageName;
                xDoc.DocumentElement.SelectSingleNode("InsertedBy").InnerText = (string) Session["sun"];
                xDoc.DocumentElement.SelectSingleNode("InsertedOn").InnerText = DateTime.Now.ToString("yyyy-MM-dd: HH:mm:ss.fff");
                xDoc.Save(xmlFile);
            }

No comments:

Post a Comment