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. Android WifiDirect 学习(三) 一些基础知识和问题

    P2P架构介绍 P2P架构中定义了三个组件,一个设备,两种角色.这三个组件分别是: P2P Device:它是P2P架构中角色的实体,读者可把它当做一个Wi-Fi设备. P2P Group Owner ...

  2. angularjs——工具方法

    1.fromJson 把json字符串转成JSON对象 var jsonStr='[{"Name":"abc","age":12},{&qu ...

  3. jquery1.9学习笔记 之选择器(基本元素三)

    标签选择器("element") 描述: 选择所有与给出标签名相匹配的元素. 同功能的JS原生方法:getElementByTagName() 例子:  查找每个div元素. &l ...

  4. google 地图,多个标记 js库

    360 云盘:http://yunpan.cn/cVgU3X7JFxAGY (提取码:1f07) 百度云盘:链接: http://pan.baidu.com/s/1c0fbCWw 密码: w1pm 参 ...

  5. dedecms 织梦ping服务插件 最新破解可用版

    dedecms 织梦ping服务插件 最新破解可用版  ping_gbk.xml <module> <baseinfo> name=ping服务 team=井哥 time=20 ...

  6. flask开发restful api系列(2)

    继续上一章所讲,上一章我们最后面说道,虽然这个是很小的程序,但还有好几个要优化的地方.先复制一下老的view.py代码. # coding:utf-8 from flask import Flask, ...

  7. 有时summary的状态和details是否有open属性有关

    用过mac的同学对这个界面一定很熟悉,因为这个界面和我们今天要说的details有很多相近的地方,首先,其有折叠效果,用户可以自己选择打开或关闭哪一个,其次,当我们直接打开的时候,默认会有几个标签是打 ...

  8. java decompiler如何去掉行号

    今天想反编译jar包保存源代码,默认前面加了行号,不知道这个小工具如何设置去掉行号? 反编译后: 找到它的安装路径,我的是:C:\Program Files\decomp.然后将该路径加入到环境变量p ...

  9. 深入浅出Node.js (8) - 构建Web应用

    8.1 基础功能 8.1.1 请求方法 8.1.2 路径解析 8.1.3 查询字符串 8.1.4 Cookie 8.1.5 Session 8.1.6 缓存 8.1.7 Basic认证 8.2 数据上 ...

  10. 【转】如何调整visio绘图区域尺寸大小

    原文网址:http://jingyan.baidu.com/article/948f5924033870d80ff5f9f1.html 在使用microsoft visio软件绘图时,为了绘图的质量和 ...