创建一个新的XML文件
1 private static XmlElement rootele; 2 private static XmlElement xmlele; 3 private static XmlNodeList nodelist; 4 private static XmlNode xmln; 5 private static void CreateNewXMLFile() 6 { 7 string[,] book_arr = new string[,] { { "English", "101" }, 8 { "History", "102" }, 9 { "Computer", "103" }, 10 { "Algorithm","104" } }; 11 xmldoc = new XmlDocument(); 12 xmln = xmldoc.CreateNode(XmlNodeType.XmlDeclaration,"",""); 13 xmldoc.AppendChild(xmln); 14 //add root node 15 rootele = xmldoc.CreateElement("","Books",""); 16 xmldoc.AppendChild(rootele); 17 18 for (int i = 0; i < book_arr.GetLength(0); i++) 19 { 20 //add node 21 rootele = xmldoc.CreateElement("book"); 22 //add element 23 xmlele = xmldoc.CreateElement("name"); 24 xmlele.InnerText = book_arr[i, 0].ToString(); 25 rootele.AppendChild(xmlele); 26 //add element 27 xmlele = xmldoc.CreateElement("sales"); 28 xmlele.InnerText = book_arr[i, 1].ToString(); 29 rootele.AppendChild(xmlele); 30 31 xmldoc.ChildNodes.Item(1).AppendChild(rootele); 32 } 33 xmldoc.Save(@"C:\Users\v-alzhou\Desktop\Create a new XML file.xml"); 34 }