你的位置:首页 > ASP.net教程

[ASP.net教程]Asp.Net写入读取Xml(处理文件权限)

1,网上关于读取写入

2,判断文件文件夹和文件是否存在(写入时会自动创建

public string CreateFolder()    {      string fileName = "my";      string folderPath = "C:\\Configurations";      string filePath = @"C:\\Configurations\" + fileName + ".";      if (!Directory.Exists(folderPath))      {        Directory.CreateDirectory(folderPath);        //给文件夹Everyone赋完全控制权限        DirectorySecurity folderSec = new DirectorySecurity();        folderSec.AddAccessRule(new FileSystemAccessRule("Everyone", FileSystemRights.FullControl, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow));        System.IO.Directory.SetAccessControl(folderPath, folderSec);        CreateFile(filePath);      }      else      {        CreateFile(filePath);      }      return filePath;    }

public void CreateFile(string filePath)    {      if (!File.Exists(filePath))      {        using (FileStream fs1 = new FileStream(filePath, FileMode.Create, FileAccess.Write))        {          //给          DirectorySecurity fSec = new DirectorySecurity();          fSec.AddAccessRule(new FileSystemAccessRule("Everyone", FileSystemRights.FullControl, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow));          System.IO.Directory.SetAccessControl(filePath, fSec);        }      }    }

3,文件夹和文件都创建出来以后就是写入了。

(1)

了解了这几个对象,开发起来就比较顺了。

List<Person> list = new List<Person>();      list.Add(new Person() { Name = "张三", Age = 19, Email = "[email protected]" });      list.Add(new Person() { Name = "李四", Age = 29, Email = "[email protected]" });      list.Add(new Person() { Name = "王五", Age = 39, Email = "[email protected]" });      list.Add(new Person() { Name = "赵六", Age = 9, Email = "[email protected]" });      //1.创建一个Dom对象      new //2.编写文档定义      "1.0", "utf-8", null);      xDoc.AppendChild(//3.编写一个根节点      "List");      xDoc.AppendChild(//4.循环创建Person节点      for (int i = 0; i < list.Count; i++)      {        //4.1创建一个Person元素        "Person");        = xDoc.CreateAttribute("id");        = (i + 1).ToString();        //将属性增加到Person节点中        //4.2在这里向Person节点下增加子节点        //创建Name        "Name");        = list[i].Name;        //创建Age        "Age");        = list[i].Age.ToString();        //创建一个Email节点        = xDoc.CreateElement("Email");        = list[i].Email;        //最后把Person加到根节点下        //5.将      xDoc.Save(@"C:\Configurations\my");

4,

public DataTable GetDataFromstring fileName = "my";      string filePath = @"C:\\Configurations\" + fileName + ".";      DataTable dt = this.BuildDataTable();      try      {        = new = document.DocumentElement;        dt = LoadToTreeByreturn dt;      }      catch      {        return dt;      }    }private DataTable LoadToTreeBytry      {        foreach (in rootElement.ChildNodes)        {          if (node.NodeType == = dt.NewRow();            foreach (DataColumn dc in dt.Columns)            {              dr[dc.ColumnName] = node.Attributes[dc.ColumnName] == null ? "" : node.Attributes[dc.ColumnName].Value;            }            dt.Rows.Add(dr);            //遍历二级节点            foreach (in node.ChildNodes)            {              if (subNode.NodeType == = dt.NewRow();                foreach (DataColumn dc in dt.Columns)                {                  subDr[dc.ColumnName] = subNode.Attributes[dc.ColumnName] == null ? "" : subNode.Attributes[dc.ColumnName].Value;                }                dt.Rows.Add(subDr);              }            }          }        }        return dt;      }      catch      {        return dt;      }    }