private static string _Store = LocalPathHelper.CurrentSolutionPath + "/data/bookstore.xml";
1.添加节点
/// <summary> /// 向根节点中插入一个节点 /// </summary> public static void AddOne() { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(_Store); //1.查找booksotre节点 XmlNode root = xmlDoc.SelectSingleNode("bookstore"); //2.创建book 节点 XmlElement book = xmlDoc.CreateElement("book"); book.SetAttribute("genre", "lizanhong"); book.SetAttribute("ISBN", "2-3431-4"); XmlElement title = xmlDoc.CreateElement("title"); title.InnerText = "C#入门经典"; book.AppendChild(title); XmlElement author = xmlDoc.CreateElement("author"); author.InnerText = "厚街"; book.AppendChild(author); XmlElement price = xmlDoc.CreateElement("price"); price.InnerText = "58.3"; book.AppendChild(price); //将book节点,添加到根节点 root.AppendChild(book); //保存内容 xmlDoc.Save(_Store); }
2.修改节点
/// <summary> /// 修改节点 /// </summary> public static void UpdateOne() { XmlDocument doc = new XmlDocument(); doc.Load(_Store); //遍历修改 XmlNodeList nodeList = doc.SelectSingleNode("bookstore").ChildNodes; foreach (XmlNode node in nodeList) { //将子节点类型转换为XmlEletment类型 XmlElement ele = (XmlElement)node; if (ele.GetAttribute("genre") == "lizanhong") { ele.SetAttribute("genre", "udpate礼赞红"); XmlNodeList nodeList2 = ele.ChildNodes; foreach (XmlNode node2 in nodeList2) { XmlElement ele2 = (XmlElement)node2; if (ele2.Name == "author") { ele2.InnerText = "延纳"; break; } } break; } } //保存修改 doc.Save(_Store); } /// <summary> /// 修改节点2,使用xpath /// </summary> public static void UpdateTwo() { XmlDocument doc = new XmlDocument(); doc.Load(_Store); //查询节点 //XmlNode root = doc.SelectSingleNode("bookstore"); //XmlNodeList books = doc.SelectNodes("bookstore/book"); XmlNode title = doc.SelectNodes("bookstore/book/title")[0]; title.InnerText = title.InnerText + "---xpath"; doc.Save(_Store); }
3.删除节点
/// <summary> /// 删除节点,属性,内容 /// </summary> public static void DeleteOne() { XmlDocument doc = new XmlDocument(); doc.Load(_Store); XmlNodeList nodeList = doc.SelectSingleNode("bookstore").ChildNodes; foreach (var item in nodeList) { XmlElement ele = (XmlElement)item; if (ele.GetAttribute("genre") == "fantasy") { //删除属性 ele.RemoveAttribute("genre"); } else if (ele.GetAttribute("genre") == "udpate礼赞红") { //删除该节点的全部内容 ele.RemoveAll(); } } //保存修改 doc.Save(_Store); } /// <summary> /// 删除空节点 /// </summary> public static void DeleteTwo() { XmlDocument doc = new XmlDocument(); doc.Load(_Store); XmlNode root = doc.SelectSingleNode("bookstore"); XmlNodeList nodeList = root.ChildNodes; foreach (XmlNode node in nodeList) { XmlElement ele = (XmlElement)node; if (ele.ChildNodes.Count <= 0) //只能删除直接子节点 root.RemoveChild(node); } doc.Save(_Store); }
4.查询列表
/// <summary> /// 显示所有的数据 /// </summary> public static void ShowOne() { XmlDocument doc = new XmlDocument(); doc.Load(_Store); XmlNode root = doc.SelectSingleNode("bookstore"); XmlNodeList nodeList = root.ChildNodes; foreach (var node in nodeList) { XmlElement ele = (XmlElement)node; Console.WriteLine(ele.GetAttribute("genre")); Console.WriteLine(ele.GetAttribute("ISBN")); XmlNodeList nodeList2 = ele.ChildNodes; foreach (XmlNode node2 in nodeList2) { Console.WriteLine(node2.InnerText); } } }