1,网上关于读取写入Xml的博客比较多,参考了发现提到Xml文件权限的博客比较少。因为在开发中我发现,如果文件存于一些没有权限的路径,代码是访问不到该文件,页面会报错提示403,Forbidden。意思是禁止,也就是没有权限。需要用代码给文件EveryOne赋予完全控制权限。希望我的博客能帮助一些在权限方面遇到问题的朋友。
2,判断文件文件夹和文件是否存在(写入时会自动创建Xml,但是如果没有权限,会创建失败,所以我觉得先用FileStream把文件创建出来比较保险);
public string CreateFolder() { string fileName = "myXml"; string folderPath = "C:\\Configurations"; string filePath = @"C:\\Configurations\" + fileName + ".xml"; 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)) { //给Xml文件EveryOne赋完全控制权限 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)Xml有几个重要的对象。、|XmlDocument,Xml文档对象|XmlDeclaration,Xml文档定义对象|XmlElement,Xml节点对象|XmlAttrbute,Xml节点属性对象|
了解了这几个对象,开发起来就比较顺了。
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对象 XmlDocument xDoc = new XmlDocument(); //2.编写文档定义 XmlDeclaration xmlDec = xDoc.CreateXmlDeclaration("1.0", "utf-8", null); xDoc.AppendChild(xmlDec); //3.编写一个根节点 XmlElement xmlRoot = xDoc.CreateElement("List"); xDoc.AppendChild(xmlRoot); //4.循环创建Person节点 for (int i = 0; i < list.Count; i++) { //4.1创建一个Person元素 XmlElement xmlPerson = xDoc.CreateElement("Person"); XmlAttribute xmlAttrId = xDoc.CreateAttribute("id"); xmlAttrId.Value = (i + 1).ToString(); //将属性增加到Person节点中 xmlPerson.Attributes.Append(xmlAttrId); //4.2在这里向Person节点下增加子节点 //创建Name XmlElement xmlName = xDoc.CreateElement("Name"); xmlName.InnerText = list[i].Name; xmlPerson.AppendChild(xmlName); //创建Age XmlElement xmlAge = xDoc.CreateElement("Age"); xmlAge.InnerText = list[i].Age.ToString(); xmlPerson.AppendChild(xmlAge); //创建一个Email节点 XmlElement xmlEmail = xDoc.CreateElement("Email"); xmlEmail.InnerText = list[i].Email; xmlPerson.AppendChild(xmlEmail); //最后把Person加到根节点下 xmlRoot.AppendChild(xmlPerson); } //5.将xmlDocument对象写入到文件中 xDoc.Save(@"C:\Configurations\myXml.xml");
4,Xml读取
public DataTable GetDataFromXml() { string fileName = "myXml"; string filePath = @"C:\\Configurations\" + fileName + ".xml"; DataTable dt = this.BuildDataTable(); try { XmlDocument document = new XmlDocument(); document.Load(filePath); XmlElement rootElement = document.DocumentElement; dt = LoadToTreeByXmlDocument(rootElement, dt); return dt; } catch { return dt; } } private DataTable LoadToTreeByXmlDocument(XmlElement rootElement, DataTable dt) { try { foreach (XmlNode node in rootElement.ChildNodes) { if (node.NodeType == XmlNodeType.Element) { DataRow dr = 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 (XmlNode subNode in node.ChildNodes) { if (subNode.NodeType == XmlNodeType.Element) { DataRow subDr = 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; } }
时间: 2024-10-10 04:19:52