writing data to the xml file

Topics: ASP.NET 2.0
Jul 28, 2011 at 11:00 AM

 protected void PopulateFiles()
    {

        //string s = "test";
        try
        {
           
            //get the files before uplaoding
            ArrayList arryFilesBeforeUpload = new ArrayList();
            //store all the files in the arraylist
            for (int i = 0; i < lstFiles.Items.Count; i++)
            {
                arryFilesBeforeUpload.Add(lstFiles.Items[i].Value);
            }
            Session["FilesBeforeUpload"] = arryFilesBeforeUpload;
           
           
            //lstFiles.Items.Clear();
            string galleryDir = Server.MapPath(galleryRoot + Request.QueryString["Gallery"]);
       
           
            if (Directory.Exists(galleryDir))
            {
                lstFiles.Visible = true;
                btnDelete.Visible = true;

                btnDelete.Visible = true;
                DirectoryInfo dir = new DirectoryInfo(galleryDir);
                int fileCounter;
             
              
                foreach (FileInfo fileInfo in dir.GetFiles())
                {
                    s = "document";
                    lstFiles.Items.Add(fileInfo.Name);
                    fileCounter = 0;
                   
                    //Write the newly uploaded files to XML if the user is not in administrator role
                    if (!Page.User.IsInRole("Administrators"))//&&Page.User.Identity.IsAuthenticated)
                    {
                       ArrayList arryFileAfterUpload=new ArrayList();
                       arryFileAfterUpload = (ArrayList)Session["FilesBeforeUpload"];
                        foreach (string filename in arryFileAfterUpload)
                        {
                            if (filename == fileInfo.Name)
                            {
                                break;
                            }
                            fileCounter++;
                        }
                    
                        //uploaded file is an new one
                        //the first condition checks if the filecounter mathces that of the arryFileAfterUpload. If it matches then the
                        //arryFileAfterUpload doesnot have the current file
                        //the second condition is to check if the arryFileAfterUpload is has no items but still the lstfiles has a newly uploaded file.
                        if (((fileCounter == arryFileAfterUpload.Count) || (arryFileAfterUpload.Count == 0 && lstFiles.Items.Count != 0)) && fileCounter != 0)
                        {
                            DataSet dsGallery = new DataSet();
                            DataRow dr;
                            dsGallery.ReadXml(strSourceXml);
                            if (dsGallery.Tables.Count == 0)//ds.Tables[0].Rows.Count == 0)
                            {
                                DataTable dt = dsGallery.Tables.Add("upload");
                                DataColumn dc = dt.Columns.Add("fid", Type.GetType("System.Int32"));
                                //dc.AutoIncrement = true;
                                //dc.AutoIncrementSeed = 1;
                                //dc.AutoIncrementStep = 1;
                                dt.Columns.Add("user", Type.GetType("System.String"));
                                dt.Columns.Add("folder", Type.GetType("System.String"));
                                dt.Columns.Add("fileurl", Type.GetType("System.String"));
                                dt.Columns.Add("filename", Type.GetType("System.String"));
                                dt.Columns.Add("Status", Type.GetType("System.String"));
                               
                                dsGallery.WriteXml(System.Web.HttpContext.Current.Server.MapPath(BlogSettings.Instance.StorageLocation) + "GalleryStatus.xml", XmlWriteMode.WriteSchema);
                            }
                           
                            //create dataset table and write it to xml if its empty
                            dr = dsGallery.Tables[0].NewRow();
                            dr["fid"] = (dsGallery.Tables[0].Rows.Count) + 1;
                            dr["user"] = Session["EmployeeName"].ToString();
                            dr["folder"] = dir.Name;
                            dr["fileurl"] = "~/site/Gallery/"+dir.Name+"/"+fileInfo.Name;
                            dr["filename"] = fileInfo.Name;
                            dr["Status"] = "Not Approved";
                            dsGallery.Tables[0].Rows.Add(dr);
                            dsGallery.AcceptChanges();
                            //dsGallery.WriteXml(strSourceXml);
                            dsGallery.WriteXml(System.Web.HttpContext.Current.Server.MapPath(BlogSettings.Instance.StorageLocation) + "GalleryStatus.xml", XmlWriteMode.WriteSchema);
                            //BindGrid();
                        }
                        //check if a file has beeb newly uploaded
                       
                    }
                    //string temp = Server.MapPath(galleryRoot + Request.QueryString["Gallery"] + "\\Thumbs\\" + fileInfo.Name);
                    //if (!File.Exists(temp))
                    //{
                    //    CreateThumbnails(galleryRoot + Request.QueryString["Gallery"] + "\\" + fileInfo.Name, 120, 120, galleryRoot + Request.QueryString["Gallery"] + "\\Thumbs\\", fileInfo.Name);
                    //}
                   
                }

            }
            else
            {

                btnDelete.Visible = false;
                lstFiles.Visible = false;
                btnDelete.Visible = false;
            }
        }
        catch { }
        //Console.Write(s);
    }
  this is the code for populating the files in gridview .After  updating files in the xml file it will automatically retieve the xml file .the problem is it is not updating the xml file only.