先用vs2017建立一个xml文件
<?xml version="1.0" encoding="ISO-8859-1"?> <bookstore> <!--记录书本的信息--> <book ISBN="7-111-19149-2" Type="必修课"> <title>数据结构</title> <author>严蔚敏</author> <price>30.00</price> </book> <book ISBN="7-111-19149-3" Type="必修课"> <title>路由型与交换型互联网基础</title> <author>程庆梅</author> <price>27.00</price> </book> <book ISBN="7-111-19149-4" Type="必修课"> <title>计算机硬件技术基础</title> <author>李继灿</author> <price>25.00</price> </book> <book ISBN="12345678" Type="必修课"> <title>软件质量保证与管理</title> <author>朱少民</author> <price>39.00</price> </book> </bookstore>
创建C#控制台 ,使用预处理头文件
using System;
using System.Collections.Generic;
using System.Xml;
创建book类:
public class BookModel { public BookModel() { } //所对应的课程类型 private string bookType; private string bookISBN; private string bookName; private string bookAuthor; private double bookPrice; public string BookType { get { return bookType; } set { bookType = value; } } public string BookISBN { get { return bookISBN; } set { bookISBN = value; } } public string BookName { get { return bookName; } set { bookName = value; } } public string BookAuthor { get { return bookAuthor; } set { bookAuthor = value; } } public double BookPrice { get { return bookPrice; } set { bookPrice = value; } } }
读取xml,并将他添加到 books 集合中:
static private void copy(List<BookModel> books) {//将xml加载进来 XmlDocument doc = new XmlDocument(); doc.Load(@"C:\Users\Lin\Desktop\机试2\xml\bookstore.xml"); //获取 book 节点集合 XmlNodeList bookNodes = doc.SelectNodes("bookstore/book"); foreach (XmlNode node in bookNodes) { BookModel book = new BookModel(); //获取book的属性值
book.BookISBN=node.Attributes["ISBN"].InnerText; book.BookType=node.Attributes["Type"].InnerText;
//获取 book 子节点的内容
book.BookName = node.ChildNodes[0].InnerText; book.BookAuthor = node.ChildNodes[1].InnerText; book.BookPrice = Convert.ToDouble(node.ChildNodes[2].InnerText); //插入到 books 集合中 books.Add(book); } foreach(BookModel book in books) { Console.WriteLine("{0},{1},{2}", book.BookName, book.BookAuthor, book.BookPrice); } }
向xml中添加新的 book 节点:
private static void addNewNode() { string path = @"C:\Users\Lin\Desktop\机试2\xml\bookstore.xml"; XmlDocument doc = new XmlDocument(); doc.Load(path); XmlElement root = doc.DocumentElement; //获取根节点 XmlElement newBook = doc.CreateElement("book"); //创建新的book节点 newBook.SetAttribute("ISBN", "112-1123"); //设置新 book 节点的属性,参数1为属性名 ,参数2为属性值 XmlElement title = doc.CreateElement("title"); //创建title 节点 title.InnerText = "自学自导3"; //为title 节点赋值 newBook.AppendChild(title); //将title节点附加到 新book节点后 XmlElement author = doc.CreateElement("author"); author.InnerText = "自己"; newBook.AppendChild(author); XmlElement price = doc.CreateElement("price"); price.InnerText = "13"; newBook.AppendChild(price);
root.AppendChild(newBook); //将newBook添加到xml文件的最后 doc.Save(path); }
读取到指定 节点 ,并修改值:
//读取到 book 中 author 为“朱少民”所在的 book 节点
// 朱少民 用单引号进行引用 XmlNode node3 = doc.SelectSingleNode("bookstore/book[author='朱少民']"); //修改节点的属性 node3.Attributes["ISBN"].InnerText = "12345670008"; doc.Save(@"C:\Users\Lin\Desktop\机试2\xml\bookstore.xml"); //修改节点 node3.ChildNodes[0].InnerText="朱少民的书"; doc.Save(@"C:\Users\Lin\Desktop\机试2\xml\bookstore.xml");
写入到 txt 文件中:
private static void writeText(List<BookModel> books) { //文件不存在时,自动创建 using (StreamWriter sr = new StreamWriter(@"C:\Users\Lin\Desktop\机试2\xml\test.txt")) { foreach (BookModel book in books) { sr.WriteLine("{0},{1},{2}", book.BookName, book.BookAuthor, book.BookPrice); Console.WriteLine("{0},{1},{2}", book.BookName, book.BookAuthor, book.BookPrice); } } }