惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Simon Willison's Weblog
Simon Willison's Weblog
Help Net Security
Help Net Security
P
Privacy International News Feed
T
Threat Research - Cisco Blogs
C
Cisco Blogs
C
CERT Recently Published Vulnerability Notes
NISL@THU
NISL@THU
L
LINUX DO - 热门话题
Security Latest
Security Latest
A
Arctic Wolf
G
GRAHAM CLULEY
月光博客
月光博客
S
Securelist
D
Docker
J
Java Code Geeks
T
Troy Hunt's Blog
T
Tenable Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
SecWiki News
SecWiki News
S
Security @ Cisco Blogs
量子位
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
L
LINUX DO - 最新话题
Recent Commits to openclaw:main
Recent Commits to openclaw:main
aimingoo的专栏
aimingoo的专栏
博客园 - 【当耐特】
H
Heimdal Security Blog
The Hacker News
The Hacker News
博客园 - 三生石上(FineUI控件)
Application and Cybersecurity Blog
Application and Cybersecurity Blog
N
Netflix TechBlog - Medium
Vercel News
Vercel News
Forbes - Security
Forbes - Security
B
Blog RSS Feed
H
Hackread – Cybersecurity News, Data Breaches, AI and More
IT之家
IT之家
B
Blog
MongoDB | Blog
MongoDB | Blog
博客园 - 聂微东
Google DeepMind News
Google DeepMind News
S
Secure Thoughts
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
C
Check Point Blog
云风的 BLOG
云风的 BLOG
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
T
The Blog of Author Tim Ferriss
L
Lohrmann on Cybersecurity
F
Full Disclosure
D
Darknet – Hacking Tools, Hacker News & Cyber Security
P
Proofpoint News Feed

博客园 - 农村的芬芳

PHP使用第三篇:生成数据库 使用THINKPHP产生的:No database selected [问题 从今天开始记录PHP使用的点点滴滴 留给自个看的工具提示 Oracle 跨库 查询 复制表数据 关于海量数据处理 近日用到啦progressbar控件,将其用法留一下 select into 和 insert into select 两种表复制语句 采用regsvr32注册组件后提示:没有注册 net2005中将list<>数组转换为Table 将对象数组转换成dataset windows 服务操作 转载:XML与DataSet的相互转换类 oracle 日期函数小计 如何创建自定义帐户来运行 ASP.NET 通过ASP.net程序创建域帐户故障 EnterpriseLibrary服务问题 下载文件关闭窗体之解决方法 超级郁闷之问题,请DUDU及各位大位指正错误
转xml
农村的芬芳 · 2009-02-10 · via 博客园 - 农村的芬芳

已知有一个XML文件(bookstore.xml)如下:

<?xml version="1.0" encoding="gb2312"?> 
<bookstore> 
  
<book genre="fantasy" ISBN="2-3631-4"> 
    
<title>Oberon's Legacy</title> 
    
<author>Corets, Eva</author> 
    
<price>5.95</price> 
  
</book> 
</bookstore> 

1、往<bookstore>节点中插入一个<book>节点:

XmlDocument xmlDoc=new XmlDocument(); 
xmlDoc.Load(
"bookstore.xml"); 
XmlNode root
=xmlDoc.SelectSingleNode("bookstore");//查找<bookstore>
XmlElement xe1=xmlDoc.CreateElement("book");//创建一个<book>节点 
xe1.SetAttribute("genre","陶维佳");//设置该节点genre属性 
xe1.SetAttribute("ISBN","2-3631-4");//设置该节点ISBN属性 

XmlElement xesub1
=xmlDoc.CreateElement("title"); 
xesub1.InnerText
="CS从入门到精通";//设置文本节点 
xe1.AppendChild(xesub1);//添加到<book>节点中 
XmlElement xesub2=xmlDoc.CreateElement("author"); 
xesub2.InnerText
="张三"
xe1.AppendChild(xesub2); 
XmlElement xesub3
=xmlDoc.CreateElement("price"); 
xesub3.InnerText
="58.3"
xe1.AppendChild(xesub3); 

root.AppendChild(xe1);
//添加到<bookstore>节点中 
xmlDoc.Save("bookstore.xml"); 

结果为:

<?xml version="1.0" encoding="gb2312"?> 
<bookstore> 
  
<book genre="fantasy" ISBN="2-3631-4"> 
    
<title>Oberon's Legacy</title> 
    
<author>Corets, Eva</author> 
    
<price>5.95</price> 
  
</book> 
  
<book genre="陶维佳" ISBN="2-3631-4"> 
    
<title>CS从入门到精通</title> 
    
<author>张三</author> 
    
<price>58.3</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 xn1 in nls)//遍历 
    
      XmlElement xe2
=(XmlElement)xn1;//转换类型 
      if(xe2.Name=="author")//如果找到 
      
      xe2.InnerText
="比尔盖茨";//则修改 
      break;//找到退出来就可以了 
      }
 
    }
 
    
break
    }
 
  }
 

  xmlDoc.Save(
"bookstore.xml");//保存。

最后结果为:

<?xml version="1.0" encoding="gb2312"?> 
<bookstore> 
  
<book genre="fantasy" ISBN="2-3631-4"> 
    
<title>Oberon's Legacy</title> 
    
<author>Corets, Eva</author> 
    
<price>5.95</price> 
  
</book> 
  
<book genre="update陶维佳" ISBN="2-3631-4"> 
    
<title>CS从入门到精通</title> 
    
<author>比尔盖茨</author> 
    
<price>58.3</price> 
  
</book> 
</bookstore> 

3、删除 <book genre="fantasy" ISBN="2-3631-4">节点的genre属性,删除 <book genre="update陶维佳" ISBN="2-3631-4">节点。

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="1.0" encoding="gb2312"?> 
<bookstore> 
  
<book ISBN="2-3631-4"> 
    
<title>Oberon's Legacy</title> 
    
<author>Corets, Eva</author> 
    
<price>5.95</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 xnf1
=xe.ChildNodes; 
    
foreach(XmlNode xn2 in xnf1) 
    

    Console.WriteLine(xn2.InnerText);
//显示子节点点文本 
    }
 
  }