国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Asp.Net寫入讀取Xml(處理文件權限)

2019-11-17 02:07:12
字體:
來源:轉載
供稿:網友

asp.net寫入讀取xml(處理文件權限)

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


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 灌云县| 张家口市| 衡南县| 奈曼旗| 梁河县| 南宁市| 新兴县| 永川市| 黑水县| 大新县| 丁青县| 镇康县| 上思县| 交口县| 长治市| 偏关县| 英山县| 凤翔县| 佳木斯市| 林州市| 寿宁县| 侯马市| 永胜县| 盐城市| 漳浦县| 江川县| 泸定县| 图片| 思南县| 恩施市| 宁化县| 襄汾县| 沂源县| 凤山县| 泸水县| 禹州市| 石狮市| 五大连池市| 汕尾市| 淳化县| 井研县|