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

推荐订阅源

freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Jina AI
Jina AI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
Troy Hunt's Blog
T
The Exploit Database - CXSecurity.com
Microsoft Security Blog
Microsoft Security Blog
V
Visual Studio Blog
F
Fortinet All Blogs
博客园_首页
P
Proofpoint News Feed
V
Vulnerabilities – Threatpost
The Cloudflare Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
H
Heimdal Security Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
A
About on SuperTechFans
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
AI
AI
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
S
Security Affairs
The Register - Security
The Register - Security
S
Security @ Cisco Blogs
Hugging Face - Blog
Hugging Face - Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 聂微东
Schneier on Security
Schneier on Security
WordPress大学
WordPress大学
Google DeepMind News
Google DeepMind News
GbyAI
GbyAI
T
Tailwind CSS Blog
Hacker News: Ask HN
Hacker News: Ask HN
W
WeLiveSecurity
D
Docker
L
LangChain Blog
B
Blog RSS Feed
The Last Watchdog
The Last Watchdog
Cloudbric
Cloudbric
TaoSecurity Blog
TaoSecurity Blog
N
Netflix TechBlog - Medium
酷 壳 – CoolShell
酷 壳 – CoolShell
I
InfoQ
The Hacker News
The Hacker News
AWS News Blog
AWS News Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
宝玉的分享
宝玉的分享
I
Intezer
云风的 BLOG
云风的 BLOG
V2EX - 技术
V2EX - 技术
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO

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