将gridview中的函数更新为xml

时间:2011-07-28 10:02:39

标签: .net xml

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);
}

这是用于在gridview中填充文件的代码。更新xml文件中的文件后,它将自动撤消xml文件。问题是它不是仅更新xml文件。

0 个答案:

没有答案