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")[];
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 <= )
//只能删除直接子节点
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);
}
}
}

C# XML,XmlDocument简单操作实例的更多相关文章

  1. Linq对XML的简单操作

    前两章介绍了关于Linq创建.解析SOAP格式的XML,在实际运用中,可能会对xml进行一些其它的操作,比如基础的增删该查,而操作对象首先需要获取对象,针对于DOM操作来说,Linq确实方便了不少,如 ...

  2. XML系列之--对电文格式XML的简单操作(三)

    前两章介绍了关于Linq创建.解析SOAP格式的XML,在实际运用中,可能会对xml进行一些其它的操作,比如基础的增删该查,而操作对象首先需要获取对象,针对于DOM操作来说,Linq确实方便了不少,如 ...

  3. 行为驱动:Cucumber + Selenium + Java(一) - Cucumber简单操作实例

    场景(Scenarios) 场景是Cucumber结构的核心之一.每个场景都以关键字“Scenario:”(或本地化一)开头,后面是可选的场景标题.每个Feature可以有一个或多个场景,每个场景由一 ...

  4. 使用spring框架的JdbcTemplate实现对Oracle数据库的简单操作实例

    最近实现了一个小功能,针对Oracle数据库两张关联表进行查询和修改,因为比较简单,所以选择了spring框架里的JdbcTemplate.JdbcTemplate算是老古董了,是当年spring为了 ...

  5. WPF对于xml的简单操作(下)绑定ListView

    上个月做好的,电脑给盗了,没及时存在网盘,也及时发到随笔,于是乎悲哉!搞了一个上午终于绑定好了,有时候就是这么眼瞎,Path和XPath全瞎了,摸滚了一个上午,赶紧的随笔跟上先. <ListVi ...

  6. WPF对于xml的简单操作(上)

    private void button1_Click(object sender, RoutedEventArgs e) { XmlTextWriter writer = new XmlTextWri ...

  7. WPF对于xml的简单操作(下下)插入节点并排序

    正如T所说,下下,这个方法不堪入目, ̄□ ̄|| 贴上再说 //先搞个struct声明 private struct datastruct { public string x; public strin ...

  8. node.js下mongoose简单操作实例

    Mongoose API : http://mongoosejs.com/docs/api.html // mongoose 链接var mongoose = require('mongoose'); ...

  9. [转] node.js下mongoose简单操作实例

    Mongoose API : http://mongoosejs.com/docs/api.html // mongoose 链接 var mongoose = require('mongoose') ...

随机推荐

  1. C# List<T>转为 DataTable

    // remove "this" if not on C# 3.0 / .NET 3.5 public static System.Data.DataTable ConvertTo ...

  2. IActiveView 接口 - 浅谈

    IActiveView 和 IMap以其 Map, PageLayout之间的关系. 在ArcMap中, PageLayout 和 Map分别对应不同的视图: layout 和data view.在同 ...

  3. JQuery控制input的readonly和disabled属性

    jquery设置元素的readonly和disabled Jquery的api中提供了对元素应用disabled和readonly属性的方法,在这里记录下.如下: 1.readonly   $('in ...

  4. JS原型的剖析与理解

    原型相关的概念 关于面向对象的概念 类 class 在js中就是构造函数 在传统的面向对象语言中,使用一个叫类的东西定义模版,然后使用模版创建对象 在构造方法中也具有类似的功能,因此称其为类 实例与对 ...

  5. 【object-c基础】Object-c基础之三:面对对象开发@interface,@implementation

    1.@interface 在java等语言编程中,创建类都是用class,但在object-c中,用@interface. 例子: @interface circle :NSObject    //定 ...

  6. Swift类和结构体定义-备

    Swift中的类和结构体定义的语法是非常相似的.类使用class关键词定义类,使用struct关键词定义结构体,它们的语法格式如下: class 类名 { 定义类的成员 } struct 结构体名 { ...

  7. Lintcode--006(交叉字符串)

    Given three strings: s1, s2, s3, determine whether s3 is formed by the interleaving of s1 and s2. Ex ...

  8. 成为一名优秀的C程序员

    英文原文:To become a good C programmer 问题的提出 每过一段时间我总会收到一些程序员发来的电子邮件,他们会问我是用什么编程语言来编写自己的游戏的,以及我是如何学习这种编程 ...

  9. 从汇编来看c语言

    一. 学习过程 从C语言的角度提出一些问题,这些问题再从汇编的角度考虑,还真的很有意思. (1) 我们用高级语言编程时,一般不可能不用到变量,但是一定要用到变量吗?还有这些变量从汇编的角度是怎么实现的 ...

  10. 使用jcifs.smb.SmbFile读取Windows上共享目录的文件

    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws Servl ...