标签:
XML在平常生活中用得很多,它的结构很简单,跟windows explorer有点像。
对它进行操作主要有三种方式:XmlDocument,
假设有这么一个XML文件Book.XML
1 <?xml version="1.0" encoding="utf-8"?> 2 <bookstore> 3 <!--记录书本的信息--> 4 <book Type="必修课" ISBN="7-11-19149-2"> 5 <title>数据结构</title> 6 <author>严蔚敏</author> 7 <price>30.00</price> 8 </book> 9 <book Type="必修课" ISBN="7-111-19149-3"> 10 <title>路由型与交换型互联网基础</title> 11 <author>程青梅</author> 12 <price>27.00</price> 13 </book> 14 <book Type="必修课" ISBN="7-111-19149-4"> 15 <title>计算机硬件技术基础</title> 16 <author>李基灿</author> 17 <price>25.00</price> 18 </book> 19 <book Type="必修课" ISBN="7-111-19149-5"> 20 <title>软件质量保证与管理</title> 21 <author>朱少敏</author> 22 <price>39.00</price> 23 </book> 24 <book Type="必修课" ISBN="7-111-19149-6"> 25 <title>算法设计与分析</title> 26 <author>王红梅</author> 27 <price>23.00</price> 28 </book> 29 <book Type="选修课" ISBN="7-111-19149-1"> 30 <title>计算机操作系统</title> 31 <author>林美苏</author> 32 <price>28.00</price> 33 </book> 34 </bookstore>
1. XmlDocument
分别演示了读取,增加,修改和删除
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Xml; 6 7 namespace ConsoleTest 8 { 9 public class BookModel 10 { 11 public BookModel() 12 { 13 } 14 /// <summary> 15 /// 所对应的课程类型 16 /// </summary> 17 private string bookType; 18 public string BookType 19 { 20 get { return bookType; } 21 set { bookType = value; } 22 } 23 24 private string bookISBN; 25 public string BookISBN 26 { 27 get { return bookISBN; } 28 set { bookISBN = value; } 29 } 30 31 private string bookName; 32 public string BookName 33 { 34 get { return bookName; } 35 set { bookName = value; } 36 } 37 38 private string bookAuthor; 39 public string BookAuthor 40 { 41 get { return bookAuthor; } 42 set { bookAuthor = value; } 43 } 44 45 private double bookPrice; 46 public double BookPrice 47 { 48 get { return bookPrice; } 49 set { bookPrice = value; } 50 } 51 52 static private void showXmlInfo(string path) 53 { 54 XmlDocument doc = new XmlDocument(); 55 XmlReaderSettings settings = new XmlReaderSettings(); 56 settings.IgnoreComments = true; 57 XmlReader reader = XmlReader.Create(path, settings); 58 doc.Load(reader); 59 60 XmlNode xn = doc.SelectSingleNode("bookstore"); 61 XmlNodeList xnl = xn.ChildNodes; 62 List<BookModel> bookModelList = new List<BookModel>(); 63 foreach (XmlNode xmlNode in xnl) 64 { 65 BookModel bookModel = new BookModel(); 66 XmlElement xe = (XmlElement)xmlNode; 67 bookModel.BookISBN = xe.GetAttribute("ISBN").ToString(); 68 bookModel.BookType = xe.GetAttribute("Type").ToString(); 69 XmlNodeList xnlChild = xe.ChildNodes; 70 bookModel.BookName = xnlChild.Item(0).InnerText; 71 bookModel.BookAuthor = xnlChild.Item(1).InnerText; 72 bookModel.BookPrice = Convert.ToDouble(xnlChild.Item(2).InnerText); 73 bookModelList.Add(bookModel); 74 } 75 foreach (BookModel book in bookModelList) 76 { 77 Console.WriteLine("Book ISBN: {0} Type: {1}", book.BookISBN, book.BookType); 78 Console.WriteLine("\tBookName: {0}", book.BookName); 79 Console.WriteLine("\tBookAuthor: {0}", book.BookAuthor); 80 Console.WriteLine("\tBookPrice: {0}", book.BookPrice); 81 } 82 reader.Close(); 83 } 84 85 static void Main(string[] args) 86 { 87 const string PATH = @"C:\Users\Administrator\Desktop\Demo\Book.XML"; 88 89 Console.WriteLine("Initialization...\n"); 90 showXmlInfo(PATH); 91 92 93 XmlDocument doc = new XmlDocument(); 94 doc.Load(PATH); 95 XmlNode root = doc.SelectSingleNode("bookstore"); 96 97 XmlElement xelKey = doc.CreateElement("book"); 98 xelKey.SetAttribute("Type", "选修课"); 99 xelKey.SetAttribute("ISBN", "7-111-19149-7"); 100 101 XmlElement xelTitle = doc.CreateElement("title"); 102 xelTitle.InnerText = "计算机算法与结构"; 103 XmlElement xelAuthor = doc.CreateElement("author"); 104 xelAuthor.InnerText = "程晓旭"; 105 XmlElement xelPrice = doc.CreateElement("price"); 106 xelPrice.InnerText = "50.00"; 107 xelKey.AppendChild(xelTitle); 108 xelKey.AppendChild(xelAuthor); 109 xelKey.AppendChild(xelPrice); 110 111 root.AppendChild(xelKey); 112 doc.Save(PATH); 113 Console.WriteLine("\nApending one child...\n"); 114 showXmlInfo(PATH); 115 116 xelKey.GetElementsByTagName("title").Item(0).InnerText = ".NET深入浅出"; 117 xelKey.GetElementsByTagName("author").Item(0).InnerText = "冯小兵"; 118 xelKey.GetElementsByTagName("price").Item(0).InnerText = "49.00"; 119 doc.Save(PATH); 120 121 Console.WriteLine("\nEditting one child...\n"); 122 showXmlInfo(PATH); 123 124 xelKey.ParentNode.RemoveChild(xelKey); 125 doc.Save(PATH); 126 Console.WriteLine("\nRemove one child...\n"); 127 showXmlInfo(PATH); 128 } 129 } 130 }
标签:
原文地址:http://www.cnblogs.com/yingzhongwen/p/4629983.html