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. windows server 2003 AD

    本文转载:http://www.cnblogs.com/zfanlong1314/admin/EditPosts.aspx?opt=1 今天教大家用windows server2003系统建立Acti ...

  2. C#小写人民币转大写

    public string GetRMB(decimal moneyAmount) { string s = moneyAmount.ToString("#L#E#D#C#K#E#D#C#J ...

  3. 《图解CSS3》——笔记(二)

    作者:大漠 勘误:http://www.w3cplus.com/book-comment.html 2014年7月15日15:58:11 第二章  CSS3选择器 2.1  认识CSS选择器 2.1. ...

  4. 一个Hadoop难以查找的错误

    This script is Deprecated. Instead use start-dfs.sh and start-yarn.sh Starting namenodes on [Master1 ...

  5. iOS学习之自定义UItableViewCell

    在项目开发中,大部分情况下我们都需要自定义UITableViewCell, 今天就重点整理一下目前自己已经学过的自定义Cell的一些注意事项; 分步骤来写吧: 1.将自定义的Cell定义为属性; 2. ...

  6. Greatest common divisor(gcd)

    欧几里得算法求最大公约数 If A = 0 then GCD(A,B)=B, since the GCD(0,B)=B, and we can stop. If B = 0 then GCD(A,B) ...

  7. 最近用的几个sql语句

    都在sqlserver数据库下操作,注意sqlserver与mysql和oracle的语法区别 用惯了mysql 和oracle,突然改用sqlserver,有诸多的不习惯,诸多的坑爹,好多的坑,一一 ...

  8. SDWebImage使用——一个可管理远程图片加载的类库

    SDWebImage使用——一个可管理远程图片加载的类库     SDWebImage使用——一个可管理远程图片加载的类库 SDWebImage托管在github上.https://github.co ...

  9. FJ省队集训DAY3 T1

    思路:我们考虑如果取掉一个部分,那么能影响到最优解的只有离它最近的那两个部分. 因此我们考虑堆维护最小的部分,离散化离散掉区间,然后用线段树维护区间有没有雪,最后用平衡树在线段的左右端点上面维护最小的 ...

  10. 动态规划——min/max的单调性优化总结

    一般形式: $max\{min(ax+by+c,dF(x)+eG(y)+f)\},其中F(x)和G(y)是单调函数.$ 或 $min\{max(ax+by+c,dF(x)+eG(y)+f)\},其中F ...