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') ...
随机推荐
- channel c3 disabled, job failed on it will be run on another channel
今天执行备份时,报错: allocated channel: c3 channel c3: sid=131 instance=orcl2 devtype=DISK RMAN-03009: fail ...
- C# TextBox控件 显示大量数据
串口通信:在使用TextBox空间显示数据时,因为要显示大量的接收到的数据,当数据量大且快速(串口1ms发送一条数据)时,使用+=的方式仍然会造成界面的卡顿(已使用多线程处理),但使用AppendTe ...
- 武汉科技大学ACM :1001: A + B Problem
Problem Description Calculate A + B. Input Each line will contain two integers A and B. Process to e ...
- [Leetcode][020] Valid Parentheses (Java)
题目在这里: https://leetcode.com/problems/valid-parentheses/ [标签]Stack; String [个人分析]这个题应该算是Stack的经典应用.先进 ...
- Canvas -画图
2014-09-30 09:14:57 <!doctype html> <html> <head> <title> </title> < ...
- 浅析JavaScript和PHP中三个等号(===)和两个等号(==)的区别
先做个简单的介绍,让先有个直观的认识 == equality 等同 === identity 恒等 == 两边值类型不同的时候,要先进行类型转换,再比较. === 不做类型转换,类型不同的一定不等. ...
- 这样就算会了PHP么?-2
学些关于函数FUNCTION方面的东东.. 感觉和PYTHON的APLLY功能差不多.. <?php function come(){ echo "来啦<p>"; ...
- UART与USART的区别
UART与USART都是单片机上的串口通信,他们之间的区别如下: 首先从名字上看: UART:universal asynchronous receiver and transmitter通用异步收/ ...
- Powershell Switch 条件
Powershell Switch 条件 6 21 1月, 2012 在 Powershell tagged Powershell教程/ 分支/ 字符串/ 数字/ 条件by Mooser Lee 本 ...
- Windows Azure 存储管理器 (2014)
Windows Azure存储用户经常希望能够在"管理器"中查看他们的数据,管理器指的是一款可用于显示存储帐户数据的工具.我们之前提供了我们所知的存储管理器列表.在本文中,我 ...