//通过代码创建XML文档 //引用命名空间using System.Xml; //创建xml对像 XmlDocument doc = new XmlDocument(); //添加描述到文档 XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null); doc.AppendChild(dec); //创建根节点 XmlElement Books = doc.CreateElement("Books"); //把根节点添加到文档 doc.AppendChild(Books); //给根节点创建子节点 XmlElement book1 = doc.CreateElement("book"); //把book添加到根结点 Books.AppendChild(book1); //给book1添加子节点 XmlElement name1 = doc.CreateElement("Name"); name1.InnerText = "第一本书"; book1.AppendChild(name1); XmlElement pri1 = doc.CreateElement("Pri"); pri1.InnerText = "110"; book1.AppendChild(pri1); XmlElement con1 = doc.CreateElement("Con"); con1.InnerText = "很好看"; book1.AppendChild(con1); //给根节点创建子节点2 XmlElement book2= doc.CreateElement("book"); //把book添加到根结点 Books.AppendChild(book2); //给book1添加子节点 XmlElement name2= doc.CreateElement("Name"); name2.InnerText = "第二本书"; book2.AppendChild(name1); XmlElement pri2 = doc.CreateElement("Pri"); pri2.InnerText = "220"; book2.AppendChild(pri1); XmlElement con2 = doc.CreateElement("Con"); con2.InnerText = "一点也不很好看"; book2.AppendChild(con1); doc.Save("xml.xml");
输出结果
<?xml version="1.0" encoding="utf-8"?> <Books> <book> </book> <book> <Name>第一本书</Name> <Pri>110</Pri> <Con>很好看</Con> </book> </Books>
//通过代码创建XML文档 //引用命名空间using System.Xml; //创建xml对像 XmlDocument doc = new XmlDocument(); //添加描述到文档 XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null); doc.AppendChild(dec); //创建根节点 XmlElement Books = doc.CreateElement("Books"); //把根节点添加到文档 doc.AppendChild(Books); //给根节点创建子节点 XmlElement book1 = doc.CreateElement("book"); //把book添加到根结点 Books.AppendChild(book1); //给book1添加子节点 XmlElement name1 = doc.CreateElement("Name"); name1.InnerText = "第一本书"; book1.AppendChild(name1); XmlElement pri1 = doc.CreateElement("Pri"); pri1.InnerText = "110"; book1.AppendChild(pri1); XmlElement con1 = doc.CreateElement("Con"); con1.InnerText = "很好看"; book1.AppendChild(con1); //创建新节点 XmlElement xlist = doc.CreateElement("list"); book1.AppendChild(xlist); XmlElement xitem1 = doc.CreateElement("item"); //创建添加属性 xitem1.SetAttribute("name", "这是什么啊"); xitem1.SetAttribute("conut", "100"); xlist.AppendChild(xitem1); //创建新节点 XmlElement xitem2 = doc.CreateElement("item"); //创建添加属性 xitem2.SetAttribute("name", "不知道"); xitem2.SetAttribute("conut", "1001"); xlist.AppendChild(xitem2); doc.Save("xml.xml");
输出结果
<?xml version="1.0" encoding="utf-8"?> <Books> <book> <Name>第一本书</Name> <Pri>110</Pri> <Con>很好看</Con> <list> <item name="这是什么啊" conut="100" /> <item name="不知道" conut="1001" /> </list> </book> </Books>
添加节点
//通过代码创建XML文档 //引用命名空间using System.Xml; //创建xml对像 XmlDocument doc = new XmlDocument(); XmlElement Books; //判断文档是否存在 if (File.Exists("xml.xml")) { //加载文档 doc.Load("xml.xml"); Books = doc.DocumentElement; } else { //添加描述到文档 XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null); doc.AppendChild(dec); //创建根节点 Books = doc.CreateElement("Books"); //把根节点添加到文档 doc.AppendChild(Books); } //给根节点创建子节点 XmlElement book1 = doc.CreateElement("book"); //把book添加到根结点 Books.AppendChild(book1); //给book1添加子节点 XmlElement name1 = doc.CreateElement("Name"); name1.InnerText = "第一本书"; book1.AppendChild(name1); XmlElement pri1 = doc.CreateElement("Pri"); pri1.InnerText = "110"; book1.AppendChild(pri1); XmlElement con1 = doc.CreateElement("Con"); con1.InnerText = "很好看"; book1.AppendChild(con1); //创建新节点 XmlElement xlist = doc.CreateElement("list"); book1.AppendChild(xlist); XmlElement xitem1 = doc.CreateElement("item"); //创建添加属性 xitem1.SetAttribute("name", "这是什么啊"); xitem1.SetAttribute("conut", "100"); xlist.AppendChild(xitem1); //创建新节点 XmlElement xitem2 = doc.CreateElement("item"); //创建添加属性 xitem2.SetAttribute("name", "不知道"); xitem2.SetAttribute("conut", "1001"); xlist.AppendChild(xitem2); doc.Save("xml.xml");
遍历读取
//创建对象 XmlDocument xdoc = new XmlDocument(); //选择要读取的xml xdoc.Load("xml.xml"); //获得根节点 XmlElement Books = xdoc.DocumentElement; //获得子节点 XmlNodeList xnodelist = Books.ChildNodes; foreach (XmlNode item in xnodelist) { foreach (XmlNode item1 in item) { Console.WriteLine(item1.InnerText); } { } } Console.ReadKey();
属性读取
//创建对象 XmlDocument xdoc = new XmlDocument(); //选择要读取的xml xdoc.Load("xml.xml"); //获得子节点 XmlNodeList xnodelist = xdoc.SelectNodes("/Books/book/list/item"); foreach (XmlNode item in xnodelist) { Console.WriteLine(item.Attributes["name"].Value); Console.WriteLine(item.Attributes["conut"].Value); } Console.ReadKey();
删除节点
//创建对象 XmlDocument xdoc = new XmlDocument(); //选择要读取的xml xdoc.Load("xml.xml"); //获得要删除的节点 XmlNode xnode=xdoc.SelectSingleNode("/Books/book/list"); xnode.RemoveAll(); xdoc.Save("xml.xml"); Console.WriteLine("删除成功"); Console.ReadKey();