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

推荐订阅源

Attack and Defense Labs
Attack and Defense Labs
Y
Y Combinator Blog
阮一峰的网络日志
阮一峰的网络日志
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 聂微东
V
Visual Studio Blog
量子位
博客园 - 三生石上(FineUI控件)
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Jina AI
Jina AI
宝玉的分享
宝玉的分享
Hacker News: Ask HN
Hacker News: Ask HN
小众软件
小众软件
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Hacker News - Newest:
Hacker News - Newest: "LLM"
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
W
WeLiveSecurity
Google Online Security Blog
Google Online Security Blog
AI
AI
Schneier on Security
Schneier on Security
大猫的无限游戏
大猫的无限游戏
WordPress大学
WordPress大学
I
InfoQ
A
Arctic Wolf
月光博客
月光博客
Last Week in AI
Last Week in AI
Hugging Face - Blog
Hugging Face - Blog
Cloudbric
Cloudbric
Google DeepMind News
Google DeepMind News
Security Latest
Security Latest
GbyAI
GbyAI
N
Netflix TechBlog - Medium
TaoSecurity Blog
TaoSecurity Blog
Blog — PlanetScale
Blog — PlanetScale
Scott Helme
Scott Helme
www.infosecurity-magazine.com
www.infosecurity-magazine.com
T
The Exploit Database - CXSecurity.com
博客园 - 【当耐特】
博客园 - 司徒正美
The Hacker News
The Hacker News
Cisco Talos Blog
Cisco Talos Blog
I
Intezer
云风的 BLOG
云风的 BLOG
T
Threatpost
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Cyber Attacks, Cyber Crime and Cyber Security
罗磊的独立博客
Security Archives - TechRepublic
Security Archives - TechRepublic
T
Tenable Blog

博客园 - 刚子

sql2005中Create 对于 数据库“xxx”失败解决方案 一个简单的C#多线程间同步(事件方法)的例子 俞老师在同济大学的演讲词:度过有意义的生命 [转]谈基于.net平台windows开发中的模式窗体 利用反射原理,灵活配置类名和方法名 [转]在C#隐藏启动窗口的几种方法 System.Windows.Forms.Timer和System.Timers.Timer的区别 [转] 什么是Smart Client 在VS.NET 2005中体验clickonce技术 (智能客户端) C#操作EXCEL文件 如何创建、安装和调试Windows服务 最小化到系统托盘并恢复 C#多线程应用探讨(转载) C# 泛型介绍 vs2005新建网站对话框中三种位置选项(文件系统、http、ftp)的区别? 工厂模式代码的使用解释 创建 类PetShop4.0 架构的项目 .net面试题 C#面试常见问题
C#操作XML的完整例子——XmlDocument篇
刚子 · 2008-10-21 · via 博客园 - 刚子

这是一个用c#控制台程序下,  用XmlDocument 进行XML操作的的例子,包含了查询、增加、修改、删除、保存的基本操作。较完整的描述了一个XML的整个操作流程。适合刚入门.net XML操作的朋友参考和学习。

假设有XML文件:books.xml

<?xml version="1.0" encoding="UTF-8"?>
<books>
 
<book>
  
<name>哈里波特</name>
  
<price>10</price>
  
<memo>这是一本很好看的书。</memo>
 
</book>
 
<book id="B02">
  
<name>三国演义</name>
  
<price>10</price>
  
<memo>四大名著之一。</memo>
 
</book>
 
<book id="B03">
  
<name>水浒</name>
  
<price>6</price>
  
<memo>四大名著之一。</memo>
 
</book>
 
<book id="B04">
  
<name>红楼</name>
  
<price>5</price>
  
<memo>四大名著之一。</memo>
 
</book>
</books>  

下面是为Program.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace TestXml
{
    
class Program
    
{
        
static void Main(string[] args)
        
{
            XmlElement theBook 
= null, theElem = null, root = null;
            XmlDocument xmldoc 
= new XmlDocument();
            
try
            
{
                xmldoc.Load(
"Books.xml");
                root 
= xmldoc.DocumentElement;

                
//---  新建一本书开始 ----
                theBook = xmldoc.CreateElement("book");
                theElem 
= xmldoc.CreateElement("name");
                theElem.InnerText 
= "新书";
                theBook.AppendChild(theElem);

                theElem 
= xmldoc.CreateElement("price");
                theElem.InnerText 
= "20";
                theBook.AppendChild(theElem);

                theElem 
= xmldoc.CreateElement("memo");
                theElem.InnerText 
= "新书更好看。";
                theBook.AppendChild(theElem);
                root.AppendChild(theBook);
                Console.Out.WriteLine(
"---  新建一本书开始 ----");
                Console.Out.WriteLine(root.OuterXml);
                
//---  新建一本书完成 ----

                
//---  下面对《哈里波特》做一些修改。 ----
                
//---  查询找《哈里波特》----
                theBook = (XmlElement)root.SelectSingleNode("/books/book[name='哈里波特']");
                Console.Out.WriteLine(
"---  查找《哈里波特》 ----");
                Console.Out.WriteLine(theBook.OuterXml);
                
//---  此时修改这本书的价格 -----
                theBook.GetElementsByTagName("price").Item(0).InnerText = "15";//getElementsByTagName返回的是NodeList,所以要跟上item(0)。另外,GetElementsByTagName("price")相当于SelectNodes(".//price")。
                Console.Out.WriteLine("---  此时修改这本书的价格 ----");
                Console.Out.WriteLine(theBook.OuterXml);
                
//---  另外还想加一个属性id,值为B01 ----
                theBook.SetAttribute("id""B01");
                Console.Out.WriteLine(
"---  另外还想加一个属性id,值为B01 ----");
                Console.Out.WriteLine(theBook.OuterXml);
                
//---  对《哈里波特》修改完成。 ----

                
//---  再将所有价格低于10的书删除  ----
                theBook = (XmlElement)root.SelectSingleNode("/books/book[@id='B02']");
                Console.Out.WriteLine(
"---  要用id属性删除《三国演义》这本书 ----");
                Console.Out.WriteLine(theBook.OuterXml);
                theBook.ParentNode.RemoveChild(theBook);
                Console.Out.WriteLine(
"---  删除后的XML ----");
                Console.Out.WriteLine(xmldoc.OuterXml);

                
//---  再将所有价格低于10的书删除  ----
                XmlNodeList someBooks = root.SelectNodes("/books/book[price<10]");
                Console.Out.WriteLine(
"---  再将所有价格低于10的书删除  ---");
                Console.Out.WriteLine(
"---  符合条件的书有 " + someBooks.Count + "本。  ---");

                
for (int i = 0; i < someBooks.Count; i++)
                
{
                    someBooks.Item(i).ParentNode.RemoveChild(someBooks.Item(i));
                }

                Console.Out.WriteLine(
"---  删除后的XML ----");
                Console.Out.WriteLine(xmldoc.OuterXml);

                xmldoc.Save(
"books.xml");//保存到books.xml

                Console.In.Read();
            }

            
catch (Exception e)
            
{
                Console.Out.WriteLine(e.Message);
            }

        }

    }

}