- 浏览: 398842 次
文章分类
- 全部博客 (327)
- Android (114)
- Java (13)
- Java基础 (14)
- Glib (2)
- linux (15)
- extjs (5)
- eclipse (2)
- Asterisk (56)
- MYSQL (6)
- 数据库 (9)
- PHP (7)
- C# (18)
- 杂谈~~ (1)
- web开发前端 (3)
- 网络编程 (2)
- Opensips (2)
- voip (3)
- debian (7)
- openfire (15)
- wordpress (1)
- 版本控制 (1)
- Android 线程服务广播 (1)
- SRTP (2)
- 单片机及ARM等 (3)
- 基础知识 (5)
- asp.net (2)
- 单片机 (1)
- 设计模式及架构 (1)
- 安全 (1)
- Cubieboard 草莓树莓各类派 Android开发板 (1)
- J2EE (2)
- BootStrap (3)
- BootStrap web前端开发 (1)
- web前端开发 (4)
- object-c (1)
- openwrt (8)
- 智能家居 (4)
- Node.js (4)
最新评论
-
xiaoxiecomeon:
你这个程序一存在线程的时间片相互抢占的问题,运行时间长了很可能 ...
Runnable 和 Thread -
dotjar:
我觉得话应该这么说:引用TestThread tt = new ...
Runnable 和 Thread -
dagf113225:
调用TelephonyManager的隐藏API是先参考Fra ...
Android提高第十四篇之探秘TelephonyManager
本文描述如何使用 XmlTextReader 类从文件读取可扩展标记语言 (XML)。 XmlTextReader 可对 XML 直接进行语法分析和标记,并且实施了 XML 1.0 规范以及 WWW 联合会 (W3C) XML 规范中的名称空间。 本文提供了对 XML 的快速、标记化流访问,而不是使用 XML 文档对象模型 (DOM) 等对象模型。
要求
下表概括了推荐使用的硬件、软件、网络架构以及所需的 Service Pack:
- Microsoft Visual Studio .NET
本文假定您熟悉下列主题:
- XML 术语
- 创建和读取 XML 文件
如何从文件读取 XML
本示例使用一个名为 Books.xml 的文件。您可以创建自己的 Books.xml 文件,或者使用以下文件夹中 .NET 软件开发工具包 (SDK) 快速入门包括的示例文件。
\Program Files\Microsoft Visual Studio .NET\FrameworkSDK\Samples\QuickStart\Howto\Samples\Xml\Transformxml\Cs
必须将 Books.xml 文件复制到 \Bin\Debug 文件夹中,该文件夹位于创建此项目所在文件夹的下面。也可以下载 Books.xml;有关下载位置,请参阅参考部分。
- 打开 Visual Studio .NET。
- 新建 Visual C# .NET 控制台应用程序。 您可以直接阅读完整代码列表一节,或者继续执行以下步骤生成应用程序。
- 确保项目包含了对 System.Xml.dll 程序集的引用。
- 在 System.Xml 名称空间上指定 using 指令,这样,以后就不需要在代码中限定 XmlTextReader声明了。using 指令必须位于任何其他声明之前。
using System.Xml;
- 创建一个 XmlTextReader 对象的实例,并使用 XML 文件填充该实例。 通常,如果需要将 XML 作为原始数据来访问而不产生 DOM 开销,则使用 XmlTextReader 类;因此,XmlTextReader 类提供了一种更快速读取 XML 的机制。XmlTextReader 类使用不同的构造函数来指定 XML 数据的位置。 以下代码创建一个 XmlTextReader 对象的实例,并加载 Books.xml 文件。 将以下代码添加到 Class1 的 Main 过程中:
XmlTextReader reader = new XmlTextReader ("books.xml");
- 读取全部 XML 数据。 (注意,此步骤演示了一个外部“while”循环,并在下两个步骤中演示如何使用该循环来读取 XML。) 在创建 XmlTextReader 对象后,请使用 Read 方法读取 XML 数据。 Read方法继续顺序读取 XML 文件直至到达文件结尾,此时 Read 方法返回“False”值。
while (reader.Read()) { // Do some work here on the data. Console.WriteLine(reader.Name); } Console.ReadLine();
- 检查节点。若要处理 XML 数据,每个记录都应该有一个可通过 NodeType 属性进行确定的节点类型。Name 和 Value 属性返回当前节点(或记录)的节点名(元素和属性名)和节点值(节点文本)。NodeType 枚举确定节点类型。下面的代码示例显示了元素的名称和文档类型。 注意,此示例忽略了元素属性。
while (reader.Read()) { switch (reader.NodeType) { case XmlNodeType.Element: // The node is an element. Console.Write("<" + reader.Name); Console.WriteLine(">"); break; case XmlNodeType.Text: //Display the text in each element. Console.WriteLine (reader.Value); break; case XmlNodeType. EndElement: //Display the end of the element. Console.Write("</" + reader.Name); Console.WriteLine(">"); break; } }
- 检查属性。元素节点类型可包括一系列与其关联的属性节点。MovetoNextAttribute 方法连续在元素的每个属性中移动。使用 HasAttributes 属性检测节点是否有任何属性。AttributeCount 属性返回当前节点的属性个数。
while (reader.Read()) { switch (reader.NodeType) { case XmlNodeType.Element: // The node is an element. Console.Write("<" + reader.Name); while (reader.MoveToNextAttribute()) // Read the attributes. Console.Write(" " + reader.Name + "='" + reader.Value + "'"); Console.Write(">"); Console.WriteLine(">"); break; case XmlNodeType.Text: //Display the text in each element. Console.WriteLine (reader.Value); break; case XmlNodeType. EndElement: //Display the end of the element. Console.Write("</" + reader.Name); Console.WriteLine(">"); break; } }
- 保存并关闭项目。
完整代码列表
using System;
using System.Xml;
namespace ReadXMLfromFile
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
static void Main(string[] args)
{
XmlTextReader reader = new XmlTextReader ("books.xml");
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element: // The node is an element.
Console.Write("<" + reader.Name);
Console.WriteLine(">");
break;
case XmlNodeType.Text: //Display the text in each element.
Console.WriteLine (reader.Value);
break;
case XmlNodeType.EndElement: //Display the end of the element.
Console.Write("</" + reader.Name);
Console.WriteLine(">");
break;
}
}
Console.ReadLine();
}
}
}
示例输出
<bookstore>
<book>
<title>
The Autobiography of Benjamin Franklin
</title>
<author>
<first-name>
Benjamin
</first-name>
<last-name>
Franklin
</last-name>
</author>
<price>
8.99
</price>
</book>
<book>
<title>
The Confidence Man
</title>
<author>
<first-name>
Herman
</first-name>
<last-name>
Melville
</last-name>
</author>
<price>
11.99
</price>
</book>
<book>
<title>
The Gorgias
</title>
<author>
<name>
Plato
</name>
</author>
<price>
9.99
</price>
</book>
</bookstore>
参考
下列文件可从 Microsoft 下载中心下载:
有关详细信息,请参阅“XML in .NET:.NET Framework XML Classes and C# Offer Simple, Scalable Data Manipulation”(.NET 中的 XML:.NET 框架 XML 类和 C# 提供了简单、可缩放的数据操作)一文,这篇文章在 MSDN Magazine 中,可从下面的 Microsoft Web 站点访问:
有关 XmlReader 类的更多信息,请访问下面的 .NET 框架类库 Web 站点:
有关如何使用 XmlReader 读取 XML 数据的更多信息,请参阅下面的 Microsoft .NET 框架开发人员指南文档:
发表评论
-
C# 全局异常处理
2015-10-20 13:36 1063咳,今天给大家带来一个关于C#中的异常处理,很多朋友在C#中 ... -
C#如何判断操作系统位数是32位还是64位
2014-11-07 10:50 1321方法一:对于C#来说,调用WMI是一种简单易行的方式。我们 ... -
UML类图几种关系的总结
2014-11-03 09:34 483在UML类图中,常见的有以下几种关系: 泛化(Genera ... -
Assembly类
2014-10-30 10:25 586System.Reflection.Assembly类是一个 ... -
由云台协议DLL浅谈插件程序的编写(下)
2014-10-28 13:47 417由云台协议DLL浅谈插件程序的编写(下)【原创】 1.创建 ... -
由云台协议DLL浅谈插件程序的编写(上)
2014-10-28 13:47 518前言:本文通过一个简单的工程描述了一种插件设计的思想。复杂设 ... -
平台+插件软件设计基本思想
2014-10-28 13:38 933平台+插件软件设计基 ... -
插件式设计的架构模型与实例
2014-10-28 13:38 319插件式设计近年来非常流行,其中eclipse起了推波助澜的作 ... -
C++插件架构浅谈与初步实现
2014-10-28 13:36 651一、插件架构初步介绍 ... -
什么时候用抽象类,什么时候用接口
2014-09-27 14:18 542共同点是他们都可以实现多态。 不同点在于设计抽象类和接口的目 ... -
使用 Override 和 New 关键字进行版本控制(C# 编程指南)
2014-09-24 14:10 480使用 Override 和 New 关键字进行版本控制(C# ... -
override(C# 参考)
2014-09-24 14:08 357override(C# 参考) Visual Stud ... -
接口可以实例化
2014-09-24 13:53 988接口可以实例化 ... -
C#的WinForm中如何让窗体大小不能改变
2014-08-20 14:20 1069设置FormBorderStyle属性为FixDialog即可 ... -
C#XML操作
2012-03-06 20:52 766using System.Xml; namespace ... -
c# http编程
2012-03-06 20:52 938c# http编程 2010-06-18 11:10 ... -
Visual C# HTTP协议开发
2012-03-06 20:53 1000Visual C# HTTP协议开发 HTTP协议简 ...
相关推荐
Work at your own pace through the lessons and hands-on exercises to learn how to build Web applications using Visual Basic .NET and Visual C# .NET. Then extend your expertise through additional skill...
《ASP.NET 4入门经典:涵盖C#和VB.NET(第6版)》循序渐进,逐步讲解了如何使用ASP.NET 4构建内容丰富的Web站点,并提供了大量使用C#和VB的示例。通过实际动手练习,您将学到关于构建Web站点的第一手信息,同时能够...
Work at your own pace through the lessons and hands-on exercises to learn how to build Windows-based applications using Visual Basic .NET and Visual C# .NET. Then extend your expertise through ...
Migrating to .NET: A Pragmatic Path to Visual Basic .NET, Visual C++ .NET, and ASP.NET Find out how to migrate your legacy software to the powerful Microsoft .NET framework Provides for a complete,...
The purpose of this book is to teach you from scratch how to use ASP.NET to write Web pages and Web applications in which content can be programmatically tailored each time an individual client ...
To answer this question we will dig ever deeper into the structure of the C# language and the onion-skin abstraction layers of the .NET Framework that underpins it. We’ll follow the execution thread ...
Your hands-on guide to Visual C# fundamentals Expand your expertise—and teach yourself the fundamentals of Microsoft Visual C# 2013. If you have previous programming experience but are new to Visual ...
This sample describes and shows how arrays work in C#. This sample contains the source code for the Arrays Tutorial. Building and Running the Sample Within Visual Studio To build and run the Arrays...
Work at your own pace through the lessons and hands-on exercises to learn how to build XML Web services and server components using Visual Basic®.NET and Visual C#™ .NET. Then extend your expertise...
Visual C# 2012 How to Program,Visual C# 2012 How to Program非常经典的一本教程
ASP.NET MVC 1.0 MSDN Reference CHM Version ASP.NET MVC 1.0 MSDN 参考及...How to: Add a Custom MVC Test Framework in Visual Studio ASP.NET MVC Reference -------------------- 有人嫌贵,特将资源分调整为2.
Welcome to .NET and Visual Basic .NET.As you already know, .NET is a name for a new strategy: a blueprint for building applications for the next decade. It’s actually even more than that. It’s ...
DEVELOPING APPLICATIONS WITH VISUAL STUDIO.NET is an in-depth guide that takes Windows programming to the next level: creating .NET applications that leverage the prior knowledge and experience of C++...
英文原版,学习c#的可以看一看,visaul c#(大学课程版) 第五版
Forgoing the large amounts of programming codes found in most database programming books, Practical Database Programming with Visual Basic.NET shows students and professionals both how to develop ...
Visual Basic .NET How to Program 2nd Ed.pdf Visual Basic .NET 如何编程 英文原版第二版 希望对大家有帮助
Windows Workflow Foundation is a ground-breaking addition to the core of the .NET Framework that allows you to orchestrate human and system interactions as a series of workflows that can be easily ...
"C++/CLI: The Visual C++ Language for .NET introduces Microsoft's extensions to the C++ syntax that allow you to target the common language runtime the key to the heart of the .NET 3.0 platform....
Visual C# 2008 How to Program 3rd Edition答案,英文版的。
Foundations of C++/CLI: The Visual C++ Language for .NET 3.5 introduces C++/CLI, Microsoft’s extensions to the C++ syntax that allow you to target the common language runtime, the key to the heart of...