C# XML,XmlDocument简单操作实例
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简单操作实例的更多相关文章
- Linq对XML的简单操作
前两章介绍了关于Linq创建.解析SOAP格式的XML,在实际运用中,可能会对xml进行一些其它的操作,比如基础的增删该查,而操作对象首先需要获取对象,针对于DOM操作来说,Linq确实方便了不少,如 ...
- XML系列之--对电文格式XML的简单操作(三)
前两章介绍了关于Linq创建.解析SOAP格式的XML,在实际运用中,可能会对xml进行一些其它的操作,比如基础的增删该查,而操作对象首先需要获取对象,针对于DOM操作来说,Linq确实方便了不少,如 ...
- 行为驱动:Cucumber + Selenium + Java(一) - Cucumber简单操作实例
场景(Scenarios) 场景是Cucumber结构的核心之一.每个场景都以关键字“Scenario:”(或本地化一)开头,后面是可选的场景标题.每个Feature可以有一个或多个场景,每个场景由一 ...
- 使用spring框架的JdbcTemplate实现对Oracle数据库的简单操作实例
最近实现了一个小功能,针对Oracle数据库两张关联表进行查询和修改,因为比较简单,所以选择了spring框架里的JdbcTemplate.JdbcTemplate算是老古董了,是当年spring为了 ...
- WPF对于xml的简单操作(下)绑定ListView
上个月做好的,电脑给盗了,没及时存在网盘,也及时发到随笔,于是乎悲哉!搞了一个上午终于绑定好了,有时候就是这么眼瞎,Path和XPath全瞎了,摸滚了一个上午,赶紧的随笔跟上先. <ListVi ...
- WPF对于xml的简单操作(上)
private void button1_Click(object sender, RoutedEventArgs e) { XmlTextWriter writer = new XmlTextWri ...
- WPF对于xml的简单操作(下下)插入节点并排序
正如T所说,下下,这个方法不堪入目, ̄□ ̄|| 贴上再说 //先搞个struct声明 private struct datastruct { public string x; public strin ...
- node.js下mongoose简单操作实例
Mongoose API : http://mongoosejs.com/docs/api.html // mongoose 链接var mongoose = require('mongoose'); ...
- [转] node.js下mongoose简单操作实例
Mongoose API : http://mongoosejs.com/docs/api.html // mongoose 链接 var mongoose = require('mongoose') ...
随机推荐
- python 元类与定制元类
1:元类 元类:类的创建与管理者 所有类的元类是type class a: pass print(type(a)) 结果:<class 'type'> 2:定制元类 类的实例化过程:(可看 ...
- 小技巧之jQueryMobile
使用JqueryMobile+MVC做一个手机网站,也有2个月了.有一些小小的经验,跟大伙们分享一下下 小技巧1: 禁用所有Ajax加载,它会很烦人的. $.mobile.ajaxLinksEnabl ...
- CentOS 7 之安装篇
程序员是一个学到老的行业,因为新换一个公司,感觉也轻松了好多,自己想想还是多学一些知识吧,中国政府都要强制以每年15%的比例使用国产系统,相信Linux还是有必要学习的.因为曾经在文思做Expedia ...
- js中的逻辑或和逻辑与
a=''||'abc'; //返回什么? 'abc' a=1||2; // ...
- Python一路走来 DAY15 Javascript
JavaScript是一门编程语言,浏览器内置了JavaScript语言的解释器,所以在浏览器上按照JavaScript语言的规则编写相应代码之,浏览器可以解释并做出相应的处理. 一 如何编写 ...
- python中的model模板中的数据类型
mode对应的类型 见 : https://docs.djangoproject.com/en/1.8/ref/models/fields/ 命令行ipython查看 from django.db i ...
- Net线程池设计
Net线程池设计 功能描述: 支持创建多个线程池,并统一管理 支持不同线程池的容量控制,以及最少活动线程的设置 支持不同线程池中活动线程的闲时设置,即线程空闲时间到期后即自动被回收 结构设计: Thr ...
- 神经网络及其简单实现(MATLAB)
转自:http://www.cnblogs.com/heaad/archive/2011/03/07/1976443.html 第0节.引例 本文以Fisher的Iris数据集作为神经网络程序的测试 ...
- 进程外组件通信之免注册com通信【原创】
最近在搞进程外组件通信的东西,写了个demo,免注册的,一直没调通,其实就是两个问题卡了好几天,也没找到有用的资料,试了好几天终于才解决,现简单记录下来,免得大家跟我走一样的弯路.下面com端程序名称 ...
- ubuntu安装openssh-server
openssh-server是依赖于openssh-clienr的,那ubuntu不是自带了openssh-client吗? 原因是自带的openssh-clien与所要安装的openssh-serv ...