作为一个小型的数据存储传递的工具——XML,大家肯定也不陌生,今天就关于XML的一些简单操作做些总结。
这些都是在控制台进行操作的·····
1.创建XML
1)创建普通XML
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
static void Main(string[] args) { //通过代码来创建XML文档 //1、引用命名空间 //2、创建XML文档对象 XmlDocument doc = new XmlDocument(); //3、创建第一个行描述信息,并且添加到doc文档中 XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null); doc.AppendChild(dec); //4、创建根节点 XmlElement books = doc.CreateElement("Books"); //将根节点添加到文档中 doc.AppendChild(books); //5、给根节点Books创建子节点 XmlElement book1 = doc.CreateElement("Book"); //将book添加到根节点 books.AppendChild(book1); //6、给Book1添加子节点 XmlElement name1 = doc.CreateElement("Name"); name1.InnerText = "三国演义"; book1.AppendChild(name1); XmlElement price1 = doc.CreateElement("Price"); price1.InnerText = "70"; book1.AppendChild(price1); XmlElement des1 = doc.CreateElement("Des"); des1.InnerText = "好看"; book1.AppendChild(des1); XmlElement book2 = doc.CreateElement("Book"); books.AppendChild(book2); XmlElement name2 = doc.CreateElement("Name"); name2.InnerText = "西游记"; book2.AppendChild(name2); XmlElement price2= doc.CreateElement("Price"); price2.InnerText = "80"; book2.AppendChild(price2); XmlElement des2 = doc.CreateElement("Des"); des2.InnerText = "还不错"; book2.AppendChild(des2); doc.Save("Books.xml"); Console.WriteLine("保存成功"); Console.ReadKey(); }
根据代码写的,然后运行,就会得到我们想要的XML文档:
2)创建带属性的XML
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
static void Main(string[] args) { XmlDocument doc = new XmlDocument(); XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8","yes"); doc.AppendChild(dec); XmlElement order = doc.CreateElement("Order"); doc.AppendChild(order); XmlElement customerName = doc.CreateElement("CustomerName"); customerName.InnerText = "张三"; order.AppendChild(customerName); XmlElement customerNumber = doc.CreateElement("CustomerNumber"); customerNumber.InnerText = "1010101"; order.AppendChild(customerNumber); XmlElement items = doc.CreateElement("Items"); order.AppendChild(items); XmlElement orderItem1 = doc.CreateElement("OrderItem"); //给节点添加属性 orderItem1.SetAttribute("Name", "单反"); orderItem1.SetAttribute("Count", "1120"); items.AppendChild(orderItem1); XmlElement orderItem2 = doc.CreateElement("OrderItem"); //给节点添加属性 orderItem2.SetAttribute("Name", "书"); orderItem2.SetAttribute("Count", "30"); items.AppendChild(orderItem2); XmlElement orderItem3 = doc.CreateElement("OrderItem"); //给节点添加属性 orderItem3.SetAttribute("Name", "手机"); orderItem3.SetAttribute("Count", "2000"); items.AppendChild(orderItem3); doc.Save("Order.xml"); Console.WriteLine("保存成功"); Console.ReadKey(); }
根据代码写的,然后运行,就会得到我们想要的XML文档:
2.追加XML
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
static void Main(string[] args) { //追加XML文档 XmlDocument doc = new XmlDocument(); XmlElement books; if (File.Exists("Books.xml")) { //如果文件存在 加载XML doc.Load("Books.xml"); //获得文件的根节点 books = doc.DocumentElement; } else { //如果文件不存在 //创建第一行 XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null); doc.AppendChild(dec); //创建跟节点 books = doc.CreateElement("Books"); doc.AppendChild(books); } //5、给根节点Books创建子节点 XmlElement book1 = doc.CreateElement("Book"); //将book添加到根节点 books.AppendChild(book1); //6、给Book1添加子节点 XmlElement name1 = doc.CreateElement("Name"); name1.InnerText = "c#开发大全"; book1.AppendChild(name1); XmlElement price1 = doc.CreateElement("Price"); price1.InnerText = "110"; book1.AppendChild(price1); XmlElement des1 = doc.CreateElement("Des"); des1.InnerText = "看不懂"; book1.AppendChild(des1); doc.Save("Books.xml"); Console.WriteLine("保存成功"); Console.ReadKey(); }
根据代码写的,然后运行,就会得到我们想要的XML文档:
3.读取XML
1)读取普通XML
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
static void Main(string[] args) { XmlDocument doc = new XmlDocument(); //加载要读取的XML doc.Load("Books.xml"); //获得根节点 XmlElement books = doc.DocumentElement; //获得子节点 返回节点的集合 XmlNodeList xnl = books.ChildNodes; foreach (XmlNode item in xnl) { Console.WriteLine(item.InnerText); } Console.ReadKey(); }
根据代码写的,然后运行,就会得到读取的XML结果:
2)读取带属性的XML
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
static void Main(string[] args) { //读取带属性的XML文档 XmlDocument doc = new XmlDocument(); doc.Load("Order.xml"); XmlNodeList xnl = doc.SelectNodes("/Order/Items/OrderItem"); foreach (XmlNode node in xnl) { Console.WriteLine(node.Attributes["Name"].Value); Console.WriteLine(node.Attributes["Count"].Value); } Console.ReadKey(); }
根据代码写的,然后运行,就会得到读取的XML结果:
4.修改属性的值
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
static void Main(string[] args) { //改变属性的值 XmlDocument doc = new XmlDocument(); doc.Load("Order.xml"); XmlNode xn = doc.SelectSingleNode("/Order/Items/OrderItem[@Name='单反']"); xn.Attributes["Count"].Value = "2000"; xn.Attributes["Name"].Value = "电脑"; doc.Save("Order.xml"); Console.WriteLine("保存成功"); Console.ReadKey(); }
根据代码写的,然后运行,就会得到修改后的XML结果:
5.删除XML节点
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
static void Main(string[] args) { XmlDocument doc = new XmlDocument(); doc.Load("Order.xml"); XmlNode xn = doc.SelectSingleNode("/Order/Items"); xn.RemoveAll(); doc.Save("Order.xml"); Console.WriteLine("删除成功"); Console.ReadKey(); }
根据代码写的,然后运行,就会得到修删除后的XML结果:
至此:XML简单的增删改操作就结束了·····
快乐而轻松的写代码