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 = "hl@yahoo.com" }); list.Add(new Person() { Name = "李四", Age = 29, Email = "xzl@yahoo.com" }); list.Add(new Person() { Name = "王五", Age = 39, Email = "hhw@yahoo.com" }); list.Add(new Person() { Name = "趙六", Age = 9, Email = "ys@yahoo.com" }); //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; } }
新聞熱點
疑難解答