1 static void Main(string[] args) 2 { 3 // 读XML示例 4 XmlDocument xml = new XmlDocument(); 5 xml.Load("../../XMLFile1.xml"); 6 7 XmlAttributeCollection xac = xml.SelectSingleNode("Article").FirstChild.Attributes; 8 Console.WriteLine(xac[0].Value); // 访问属性值用Value 9 10 XmlNode node = xml.SelectSingleNode("Article/author");11 Console.WriteLine(node.InnerText); // 访问节点值用InnerText12 13 //Console.Write(xml.InnerXml); // 整个xml内容14 15 // 写XML示例16 XmlDocument xml2 = new XmlDocument();17 // 声明18 XmlDeclaration decl = xml2.CreateXmlDeclaration("1.0", "utf-8", null);19 xml2.AppendChild(decl);20 // 根节点21 XmlNode root = xml2.CreateElement("Books");22 xml2.AppendChild(root);23 // 元素节点24 XmlNode book = xml2.CreateElement("Book"); 25 XmlElement title = xml2.CreateElement("Title"); 26 title.InnerText = "SQL Server"; 27 book.AppendChild(title);28 XmlElement isbn = xml2.CreateElement("ISBN"); 29 isbn.InnerText = "444444"; 30 book.AppendChild(isbn);31 XmlElement author = xml2.CreateElement("Author"); 32 author.InnerText = "jia"; 33 book.AppendChild(author);34 XmlElement price = xml2.CreateElement("Price"); 35 price.InnerText = "120"; 36 price.SetAttribute("Unit", "___FCKpd___"); 37 book.AppendChild(price); 38 root.AppendChild(book); 39 40 //Console.Write(xml2.InnerXml);41 xml2.Save("xml2.xml");42 }