Asp. Net write and read Xml (File Processing permission), asp. netxml
1. There are many blogs on the Internet about reading and writing Xml files. I have found that there are few blogs about Xml file permissions. During development, I found that if the file is stored in a path with no permissions, the code will not be able to access the file, and an error 403, Forbidden will be reported on the page. This means that you do not have the permission. You need to use code to grant full control permissions to the file EveryOne. I hope my blog can help some friends who have problems with permissions.
2. Check whether the file folder and file exist. (Xml is automatically created during writing, but creation fails if you do not have the permission. Therefore, I think it is safer to create the file using FileStream first );
Public string CreateFolder () {string fileName = "myXml"; string folderPath = "C: \ Configurations"; string filePath = @ "C: \ Configurations \ "+ fileName + ". xml "; if (! Directory. exists (folderPath) {Directory. createDirectory (folderPath); // assign the full control permission to the folder 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) {// assign the full control permission to the Xml file 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. After folders and files are created, they are written.
(1) Xml has several important objects. , | XmlDocument, Xml Document Object | XmlDeclaration, Xml document definition object | XmlElement, Xml Node object | xmlattration, Xml node attribute object |
After learning about these objects, it is easier to develop them.
List <Person> list = new List <Person> (); list. add (new Person () {Name = "James", 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 = "Zhao ", Age = 9, Email = "ys@yahoo.com"}); // 1. create a Dom object XmlDocument xDoc = new XmlDocument (); // 2. write a document to define XmlDeclaration xmlDec = xDoc. createXmlDeclaration ("1.0", "UTF-8", null); xDoc. appendChild (xmlDec); // 3. compile a root node XmlElement xmlRoot = xDoc. createElement ("List"); xDoc. appendChild (xmlRoot); // 4. create a Person node cyclically (int I = 0; I <list. count; I ++) {// 4.1 create a Person element XmlElement xmlPerson = xDoc. createElement ("Person"); XmlAttribute xmlAttrId = xDoc. createAttribute ("id"); xmlAttrId. value = (I + 1 ). toString (); // Add the attribute to xmlPerson on the Person node. attributes. append (xmlAttrId); // 4.2 Add a subnode to the Person node here // create Name XmlElement xmlName = xDoc. createElement ("Name"); xmlName. innerText = list [I]. name; xmlPerson. appendChild (xmlName); // create Age XmlElement xmlAge = xDoc. createElement ("Age"); xmlAge. innerText = list [I]. age. toString (); xmlPerson. appendChild (xmlAge); // create an Email node XmlElement xmlEmail = xDoc. createElement ("Email"); xmlEmail. innerText = list [I]. email; xmlPerson. appendChild (xmlEmail); // Add the Person to the xmlRoot under the root node. appendChild (xmlPerson);} // 5. write the xmlDocument object to the file xDoc. save (@ "C: \ tolerations \ myXml. xml ");
4. Xml reading
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 LoadToTreeByXm LDocument (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); // traverse the second-level node 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 ;}}