c#對XML文件的建立與增刪改查的示例程式碼
阿新 • • 發佈:2020-07-23
一、建立的第一種方式
//1、建立一個XML文件 XmlDocument doc = new XmlDocument(); //2、建立第一行描述資訊 XmlDeclaration dec = doc.CreateXmlDeclaration("1.0","utf-8",null); //3、將建立的第一行描述資訊新增到文件中 doc.AppendChild(dec); //4、給文件新增根節點 XmlElement Books = doc.CreateElement("Books"); doc.AppendChild(Books); XmlElement Book = doc.CreateElement("Book"); Books.AppendChild(Book); XmlElement name = doc.CreateElement("name"); name.InnerText = "水滸傳"; Book.AppendChild(name); XmlElement author = doc.CreateElement("author"); author.InnerText = "匿名"; author.SetAttribute("name","wjl"); author.SetAttribute("count","30"); Book.AppendChild(author); doc.Save("Book.xml"); Console.WriteLine("儲存成功!"); Console.ReadKey();
建立結果如下:
二、建立的第二種方式
class Program { static void Main(string[] args) { List<Student> list = new List<Student>(); list.Add(new Student(1,"wjl1",22,"男")); list.Add(new Student(2,"wjl2",21,"男")); list.Add(new Student(3,"wjl3","男")); list.Add(new Student(4,"wjl4",24,"男")); XmlDocument xmldoc = new XmlDocument(); XmlDeclaration xmldec = xmldoc.CreateXmlDeclaration("1.0",null); xmldoc.AppendChild(xmldec); XmlElement person = xmldoc.CreateElement("Person"); xmldoc.AppendChild(person); for (int i = 0; i < list.Count; i++) { XmlElement stu = xmldoc.CreateElement("student"); stu.SetAttribute("ID",list[i].Id.ToString()); person.AppendChild(stu); XmlElement name = xmldoc.CreateElement("name"); XmlElement age = xmldoc.CreateElement("age"); name.InnerText = list[i].Name; age.InnerText = list[i].Age.ToString(); stu.AppendChild(name); stu.AppendChild(age); } xmldoc.Save("Student.xml"); Console.WriteLine("Student.xml"); Console.ReadKey(); } } class Student { int id; string name; int age; string sex; public Student(int id,string name,int age,string sex) { this.id = id; this.name = name; this.age = age; this.sex = sex; } public Student() { } }
建立結果如下:
三、對XML檔案的新增
XmlDocument doc = new XmlDocument(); //首先判斷檔案是否存在,如果存在則追加否則在建立一個 if (File.Exists("Student.xml")) { //載入 doc.Load("Student.xml"); //獲取根節點,給根節點新增子節點 XmlElement person = doc.DocumentElement; XmlElement student = doc.CreateElement("student"); student.SetAttribute("ID","1"); person.AppendChild(student); XmlElement name = doc.CreateElement("name"); XmlElement age = doc.CreateElement("age"); name.InnerText = "zjs"; age.InnerText = "41"; student.AppendChild(name); student.AppendChild(age); } else { } doc.Save("Student.xml"); Console.WriteLine("Student.xml 儲存成功");
四、對XML文件的查詢、修改、刪除
方法1:
文件結構為:
if (File.Exists("order.xml")) { doc.Load("order.xml"); // 獲取根節點 XmlElement orderElement = doc.DocumentElement; XmlNodeList orderChildr = orderElement.ChildNodes; foreach (XmlNode item in orderChildr) { Console.WriteLine("節點名稱:"+ item.Name + "節點的 InnerText :" + item.InnerText); } XmlElement orderitem = orderElement["Items"]; XmlNodeList itemlist = orderitem.ChildNodes; foreach (XmlNode item in itemlist) { Console.WriteLine(item.Attributes["Name"].Value + " " + item.Attributes["Count"].Value); } } else { Console.WriteLine("檔案不存在!"); } Console.ReadKey(); // doc.Save("Student.xml"); Console.WriteLine("Student.xml 儲存成功");
方法2:
#region 使用XPath的方式來讀取XML檔案 // 獲取文件物件 XmlDocument doc = new XmlDocument(); doc.Load("order.xml"); //獲取根節點 XmlElement order = doc.DocumentElement; // 獲取單個節點 //XmlNode xn = order.SelectSingleNode(@"/Order/CustomerName"); XmlNode xn = order.SelectSingleNode(@"/Order/Items/OrderItem[@Name='碼錶']"); xn.Attributes["Count"].Value = "20"; // 修改 doc.Save("Order.xml"); Console.WriteLine(xn.Attributes["Count"].Value); Console.ReadKey(); #endregion
刪除元素指定的特性:
xn.Attributes.RemoveNamedItem("Count"); //刪除元素指定的特性
刪除子節點:
XmlNode xn = order.SelectSingleNode(@"/Order/Items"); XmlNode xnchild = order.SelectSingleNode(@"/Order/Items/OrderItem[@Name = '雨衣']"); xn.RemoveChild(xnchild); //刪除指定的子節點
刪除當前所有子節點:
xn.RemoveAll(); //刪除當前節點的所有子節點
刪除當前節點的所有特性:
xnchild.Attributes.RemoveAll();
以上就是c#對XML文件的建立與增刪改查的示例程式碼的詳細內容,更多關於c#對XML文件的建立與增刪改查的資料請關注我們其它相關文章!