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

推荐订阅源

量子位
小众软件
小众软件
S
SegmentFault 最新的问题
人人都是产品经理
人人都是产品经理
博客园 - 【当耐特】
博客园 - 三生石上(FineUI控件)
C
Check Point Blog
S
Schneier on Security
Microsoft Azure Blog
Microsoft Azure Blog
N
Netflix TechBlog - Medium
Engineering at Meta
Engineering at Meta
GbyAI
GbyAI
罗磊的独立博客
有赞技术团队
有赞技术团队
V
V2EX
Y
Y Combinator Blog
博客园 - 叶小钗
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
F
Fortinet All Blogs
W
WeLiveSecurity
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Stack Overflow Blog
Stack Overflow Blog
The Cloudflare Blog
S
Security @ Cisco Blogs
TaoSecurity Blog
TaoSecurity Blog
MyScale Blog
MyScale Blog
Hugging Face - Blog
Hugging Face - Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
www.infosecurity-magazine.com
www.infosecurity-magazine.com
PCI Perspectives
PCI Perspectives
H
Heimdal Security Blog
Schneier on Security
Schneier on Security
Security Latest
Security Latest
AWS News Blog
AWS News Blog
月光博客
月光博客
Security Archives - TechRepublic
Security Archives - TechRepublic
Recent Announcements
Recent Announcements
Google DeepMind News
Google DeepMind News
博客园 - Franky
Cisco Talos Blog
Cisco Talos Blog
T
Threat Research - Cisco Blogs
M
MIT News - Artificial intelligence
T
Troy Hunt's Blog
N
News and Events Feed by Topic
Cloudbric
Cloudbric
Scott Helme
Scott Helme
云风的 BLOG
云风的 BLOG
Attack and Defense Labs
Attack and Defense Labs

博客园 - YQM

NUnit单元测试属性介绍 C#静态构造函数 没有使用lock的Singleton How to serialize and deserialize using C# .NET - YQM Javascript调用web service 简单的英语不简单 我们应该怎样去学习和工作--写给自己 扩展方法 使用反射-动态创建对象及调用对象方法 使用XmlWriter对象 知道做什么而不是怎样做(摘抄) 生活15点,要记住! 设计模式之Command模式 使用javascript动态添加和删除table的行和列 Generate unique strings and numbers in C#(生成一个唯一的字符串和数值) Some notes Localization in ASP.NET 2.0 ASP.NET 2.0: Playing a bit with GridView "Sort Grouping" 通过TryParse来检验和转换数据类型
使用XmlReader类
YQM · 2008-07-04 · via 博客园 - YQM

1.创建XmlReader对象
XmlReader是一个抽象类,所以,要使用XmlReader类,必须使用静态方法Create,该方法返回一个XmlReader对象。Crate方法有许多重装版本,最常用的是,使用字符串参数,输入一个XmlDocument的文件名。另外一个是基于流创建。

2.Read方法
遍历文档。代码:

1XmlReader reader = XmlReader.Create("book.xml");
2while (reader.Read())
3    if (reader.NodeType == XmlNodeType.Text)
4        Console.WriteLine(reader.Value);

返回的结果:

The Autobiography of Benjamin Franklin
Benjamin
Franklin
8.99
The confidence Man
Herman
Melville
11.99
The Gorgias
Palto
9.99

3.ReadElementString方法
Read a text-only element,代码:

 1FileStream fs = new FileStream("book.xml", FileMode.Open);
 2XmlReader tr = XmlReader.Create(fs);
 3
 4while (!tr.EOF)
 5{
 6    if (tr.MoveToContent() == XmlNodeType.Element && tr.Name == "title")
 7        Console.WriteLine(tr.ReadElementString());
 8    else
 9        tr.Read();
10}

返回结果:

The Autobiography of Benjamin Franklin
The confidence Man
The Gorgias

4.XmlReader类还可以读取强类型化的数据,它有几个ReadElementContent方法,如:ReadElementContentAsDouble,ReadElementContentASBoolean等。下面代码说明如何把值读取为小数。
(注:.net3.5的方法中加了个As,更易理解)

 1XmlReader reader = XmlReader.Create("book.xml");
 2while (reader.Read())
 3{
 4    if (reader.Name == "price")
 5    {
 6        decimal price = reader.ReadElementContentAsDecimal();
 7        Console.WriteLine("Current price = " + price);
 8        price += price * 0.25M;
 9        Console.WriteLine("New price= " + price);
10    }

11    else if (reader.Name == "title")
12        Console.WriteLine(reader.ReadElementString());
13}

返回结果:

The Autobiography of Benjamin Franklin
Current price = 8.99
New price= 11.2375
The confidence Man
Current price = 11.99
New price= 14.9875
The Gorgias
Current price = 9.99
New price= 12.4875

5.读取属性数据:
在读取节点的时候,没有看到属性,因为属性不是文档结构的一部分。针对元素节点,可以检查属性是否存在,并可读取属性值。代码:

 1XmlReader reader = XmlReader.Create("book.xml");
 2while (reader.Read())
 3{
 4    if (reader.NodeType == XmlNodeType.Element)
 5    {
 6        //Console.WriteLine(reader.HasAttributes);
 7        if (reader.HasAttributes)
 8        {
 9            for (int i = 0; i < reader.AttributeCount; i++)
10                Console.WriteLine(reader.GetAttribute(i));
11        }

12    }

13}

返回结果:

autobiography
1981
1-861003-21-2
novel
1967
0-201-63361-2
philosophy
1991
1-8610001-57-6

book.xml

Code