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

推荐订阅源

D
Docker
V
V2EX
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
云风的 BLOG
云风的 BLOG
Blog — PlanetScale
Blog — PlanetScale
Recent Announcements
Recent Announcements
Last Week in AI
Last Week in AI
博客园 - Franky
Microsoft Security Blog
Microsoft Security Blog
Hugging Face - Blog
Hugging Face - Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Vercel News
Vercel News
MyScale Blog
MyScale Blog
大猫的无限游戏
大猫的无限游戏
罗磊的独立博客
H
Help Net Security
月光博客
月光博客
Martin Fowler
Martin Fowler
博客园 - 【当耐特】
宝玉的分享
宝玉的分享
P
Proofpoint News Feed
GbyAI
GbyAI
腾讯CDC
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - 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