1/定义一个XmlDocument对象xDoc
2/通过XmlDocument来load需要读取的xml文件
3/通过XmlDocument的SelectSingleNode来找到节点,并把节点转换为XmlElement
4/XmlElement 可以对节点的属性进行操作

<?xml version="." encoding="gb"?>
  <bookstore>
  <book genre="fantasy" ISBN="--">
  <title>Oberon's Legacy</title>
  <author>Corets, Eva</author>
<price>.</price>
</book>
</bookstore>
 
 
 
 
 
1、往<bookstore>节点中插入一个<book>节点:
XmlDocument xmlDoc=new XmlDocument();
xmlDoc.Load("bookstore.xml");
XmlNode root=xmlDoc.SelectSingleNode("bookstore");//查找<bookstore>

XmlElement xe=xmlDoc.CreateElement("book");//创建一个<book>节点
xe.SetAttribute("genre","李小龙");//设置该节点genre属性
xe.SetAttribute("ISBN","--");//设置该节点ISBN属性
 
XmlElement xesub=xmlDoc.CreateElement("title");
xesub.InnerText="CS从入门到精通";//设置文本节点
xe.AppendChild(xesub);//添加到<book>节点中
XmlElement xesub=xmlDoc.CreateElement("author");
xesub.InnerText="候捷";
xe.AppendChild(xesub);
XmlElement xesub=xmlDoc.CreateElement("price");
xesub.InnerText=".";
xe.AppendChild(xesub);
 
root.AppendChild(xe);//添加到<bookstore>节点中
xmlDoc.Save("bookstore.xml");
 
 
//================
结果为:
 
 
 
 
<?xml version="." encoding="gb"?>
<bookstore>
<book genre="fantasy" ISBN="--">
<title>Oberon's Legacy</title>
<author>Corets, Eva</author>
<price>.</price>
</book>
<book genre="李小龙" ISBN="--">
<title>CS从入门到精通</title>
<author>候捷</author>
<price>.</price>
</book>
</bookstore>
 
 
 
 
 
2、修改节点:将genre属性值为“李小龙“的节点的genre值改为“update李小龙”,将该节点的子节点<author>的文本修改为“亚胜”。
 
 
XmlNodeList nodeList=xmlDoc.SelectSingleNode("bookstore").ChildNodes;//获取bookstore节点的所有子节点
foreach(XmlNode xn in nodeList)//遍历所有子节点
{
XmlElement xe=(XmlElement)xn;//将子节点类型转换为XmlElement类型
if(xe.GetAttribute("genre")=="李小龙")//如果genre属性值为“李赞红”
{
xe.SetAttribute("genre","update李小龙");//则修改该属性为“update李赞红”
 
XmlNodeList nls=xe.ChildNodes;//继续获取xe子节点的所有子节点
foreach(XmlNode xn in nls)//遍历
{
XmlElement xe=(XmlElement)xn;//转换类型
if(xe.Name=="author")//如果找到
{
xe.InnerText="亚胜";//则修改
break;//找到退出来就可以了
}
}
break;
}
}
 
xmlDoc.Save("bookstore.xml");//保存。
 
 
 

//=================

最后结果为:

<?xml version="." encoding="gb"?>
<bookstore>
<book genre="fantasy" ISBN="--">
<title>Oberon's Legacy</title>
<author>Corets, Eva</author>
<price>.</price>
</book>
<book genre="update李小龙" ISBN="--">
<title>CS从入门到精通</title>
<author>亚胜</author>
<price>.</price>
</book>
</bookstore>

 

3、删除 <book genre="fantasy" ISBN="--">节点的genre属性,删除 <book genre="update李赞红" ISBN="--">节点。

XmlNodeList xnl=xmlDoc.SelectSingleNode("bookstore").ChildNodes;

foreach(XmlNode xn in xnl)
{
XmlElement xe=(XmlElement)xn;

if(xe.GetAttribute("genre")=="fantasy")
{
xe.RemoveAttribute("genre");//删除genre属性
}
else if(xe.GetAttribute("genre")=="update李小龙")
{
xe.RemoveAll();//删除该节点的全部内容
}
}
xmlDoc.Save("bookstore.xml");

//====================

最后结果为:

<?xml version="." encoding="gb"?>
<bookstore>
<book ISBN="--">
<title>Oberon's Legacy</title>
<author>Corets, Eva</author>
<price>.</price>
</book>
<book>
</book>
</bookstore>

4、显示所有数据。

XmlNode xn=xmlDoc.SelectSingleNode("bookstore");

XmlNodeList xnl=xn.ChildNodes;

foreach(XmlNode xnf in xnl)
{
XmlElement xe=(XmlElement)xnf;
Console.WriteLine(xe.GetAttribute("genre"));//显示属性值
Console.WriteLine(xe.GetAttribute("ISBN"));

XmlNodeList xnff=xe.ChildNodes;
foreach(XmlNode xn in xnff)
{
Console.WriteLine(xn.InnerText);//显示子节点点文本
}
}

c#读取xml操作的更多相关文章

  1. XML格式示例 与 XML操作(读取)类封装

    header('Content-Type: text/xml'); <?xml version="1.0" encoding="utf-8" standa ...

  2. Qt XML读取写入操作

    XML(eXtensible Markup Language,可扩展标记语言)是普通用于数据交换和数据存储的一种多用途文本文件格式: SVG(可标量矢量图形)XML格式,QtSvg模块提供了可用于载入 ...

  3. PHP下进行XML操作(创建、读取)

    PHP下可以使用DOMDocument类对XML或者HTML文件进行读写操作 更为简单的方法使用simpleXML类操作XML DOM节点分为 元素节点 属性节点 值节点 注释节点 根节点(docum ...

  4. java中XML操作:xml与string互转、读取XML文档节点及对XML节点增删改查

    一.XML和String互转: 使用dom4j程式变得很简单 //字符串转XML String xmlStr = \"......\"; Document document = D ...

  5. PowerShell 数组以及XML操作

    PowerShell基础 PowerShell数组操作 将字符串拆分成数据的操作 cls #原始字符串 $str = "abc,def,ghi,mon" #数据定义 #$StrAr ...

  6. 自己动手之使用反射和泛型,动态读取XML创建类实例并赋值

    前言: 最近小匹夫参与的游戏项目到了需要读取数据的阶段了,那么觉得自己业余时间也该实践下数据相关的内容.那么从哪入手呢?因为用的是Unity3d的游戏引擎,思来想去就选择了C#读取XML文件这个小功能 ...

  7. C#中常用的读取xml的几种方法(转)

    本文完全来源于http://blog.csdn.net/tiemufeng1122/article/details/6723764,仅作个人学习之用. XML文件是一种常用的文件格式,例如WinFor ...

  8. C#读取XML文件的基类实现

    刚到新单位,学习他们的源代码,代码里读写系统配置文件的XML代码比较老套,直接写在一个系统配置类里,没有进行类的拆分,造成类很庞大,同时,操作XML的读写操作都是使用SetAttribute和node ...

  9. XML操作类

        using System; using System.Data; using System.IO; using System.Xml; namespace DotNet.Utilities { ...

随机推荐

  1. Xilinx 7系列例化MIG IP core DDR3读写

    昨晚找了一下,发现DDR3读写在工程上多是通过例化MIG,调用生成IPcore的HDL Functional Model.我说嘛,自己哪能写出那么繁琐的,不过DDR读写数据可以用到状态机,后期再添砖加 ...

  2. 版本控制器-VSS和SVN区别

    SVN 默认的工作方式和VSS不同, VSS是[锁定-修改-解锁],VSS是一个人在改的时候必须以独占的方式签出文件,导致其他人不能够修改.用VSS经常要问同事:"改完没,签入一下" ...

  3. osx升级到10.10后,用pod install报错终于解决的方法

    先依照这个文章做:http://blog.csdn.net/dqjyong/article/details/37958067 大概过程例如以下: Open Xcode 6 Open Preferenc ...

  4. static关键字 综合小应用

    package unit4; import com.sun.java_cup.internal.version; public class Voter { private static int MAX ...

  5. SharePoint管理中心配置内容数据库

    SharePoint管理中心配置内容数据库         在SharePoint2010中,内容数据库是组织数据的核心. 是全部站点内容信息,如文档.列表数据和Web部件属性等存储的地方.默认地,内 ...

  6. 使用 NGUI Toggle 制作单选框

    好久没写了,今天来把关于NGUI的做的简单功能发上来~ 这个是做单选框的.用了新版本的NGUI后,发现没有以前的Checkbox了,在网上查了之后才知道是用Toggle代替了以前的Checkbox.现 ...

  7. python 爬虫5 Beautiful Soup的用法

    1.创建 Beautiful Soup 对象 from bs4 import BeautifulSoup html = """ <html><head& ...

  8. easyui datagrid onLoadSuccess加载两次。。

    今天使用EasyUI的datagrid时发现首次打开页面时onLoadSuccess方法执行了两次.后来发现主要问题是datagrid被初始化了两次.主要原因是一开始html中声明了dg为easyui ...

  9. Wireshark使用注意事项

    一直在使用老板的Wireshark,因为4G网络的逐步开通,越来越须要新版Wireshark来解析一些数据包. 在更换了新Wireshark的1.11.3后发现原来能够解析Gb口数据的NSIP不见了 ...

  10. FreeMarker / S2SH 各种报错解决方案

    1. org.hibernate.PropertyAccessException: IllegalArgumentException occurred while calling setter of ...