没有合适的资源?快使用搜索试试~ 我知道了~
首页C#编程中XML详解:构造、验证、处理与应用
C#编程中XML详解:构造、验证、处理与应用
需积分: 13 2 下载量 146 浏览量
更新于2024-07-16
收藏 32.78MB PDF 举报
"XML入门材料.pdf"是一本旨在为C#编程者提供全面而易懂的XML指南。该教材首先从XML的基本概念出发,引导读者了解XML的本质——一种用于数据交换的标记语言,特别强调了其在数据表示和标记方面的优势。章节一详细介绍了XML的发展历程,包括它如何克服早期数据存储和交换中的问题,以及它在提高跨平台、跨系统数据交互的互操作性方面的重要性。 在实践层面,作者阐述了XML与实际应用的关联,区分了数据与文档的概念,并列举了XML可能的应用场景,如数据库集成(通过XQuery)、网络通信(RSS、SOAP、WSDL和AJAX)。这展示了XML在现代软件开发中的多元用途。 第二章深入解析了XML的语法规则,如什么是Well-formed XML,以及如何在文本编辑器中创建和编辑XML文档。讲解了元素、属性、内容、处理指令、字符数据、空白处理和错误处理等核心概念。此外,还提到了XML Information Set的概念,这是XML解析的基础,它定义了XML文档结构的抽象表示,包括元素、属性、注释、命名空间等信息项。 该教材还关注XML文档的完整性,介绍了文档类型声明(DTD)以及其他形式的XML验证技术,如XML Schema、RELAX NG和Schematron,这些工具对于确保XML文档符合预设规范至关重要。 "XML入门材料.pdf"是一本实用的教程,不仅适合初学者理解XML的基础知识,也为有一定经验的开发者提供了深入理解和实践XML在C#项目中的应用的宝贵资源。通过阅读这本书,读者将能够掌握XML在编程中的关键技能,从而提升项目的可扩展性和数据管理能力。
资源详情
资源推荐
![](https://csdnimg.cn/release/download_crawler_static/12341539/bg10.jpg)
4
❘
CHAPTER 1 WHAT IS XML?
NOTE Although this chapter presents some short examples of XML, you aren’t
expected to understand all that’s going on just yet. The idea is simply to intro-
duce the important concepts behind the language so that throughout the book
you can see not only how to use XML, but also why it works the way it does.
STEPS LEADING UP TO XML: DATA REPRESENTATION
AND MARKUPS
There are two main uses for XML: One is a way to represent low-level data, for example confi gura-
tion fi les. The second is a way to add metadata to documents; for example, you may want to stress a
particular sentence in a report by putting it in italics or bold.
The fi rst usage for XML is meant as a replacement for the more traditional ways this has been done
before, usually by means of lists of name/value pairs as is seen in Windows’ INI or Java’s Property
fi les. The second application of XML is similar to how HTML fi les work. The document text is con-
tained in an overall container, the
<body> element, with individual phrases surrounded by <i> or <b>
tags. For both of these scenarios there has been a multiplicity of techniques devised over the years.
The problem with these disparate approaches has been more apparent than ever, since the increased
use of the Internet and extensive existence of distributed applications, particularly those that rely on
components designed and managed by different parties. That problem is one of intercommunication.
It’s certainly possible to design a distributed system that has two components, one outputting data
using a Windows INI fi le and the other which turns it into a Java Properties format. Unfortunately, it
means a lot of development on both sides, which shouldn’t really be necessary and detracts resources
from the main objective, developing new functionality that delivers business value.
XML was conceived as a solution to this kind of problem; it is meant to make passing data between
different components much easier and relieve the need to continually worry about different formats
of input and output, freeing up developers to concentrate on the more important aspects of coding
such as the business logic. XML is also seen as a solution to the question of whether fi les should be
easily readable by software or by humans; XML’s aim is to be both. You’ll be examining the distinc-
tion between data-oriented and document-centric XML later in the book, but for now let’s look a
bit more deeply into what the choices were before XML when there was need to store or communi-
cate data in an electronic format.
This section takes a mid-level look at data representation, without taking too much time to explain
low-level details such as memory addresses and the like. For the purposes here you can store data in
fi les two ways: as binary or as text.
Binary Files
A binary fi le, at its simplest, is just a stream of bits (1s and 0s). It’s up to the application that created
the binary fi le to understand what all of the bits mean. That’s why binary fi les can only be read and
produced by certain computer programs, which have been specifi cally written to understand them.
![](https://csdnimg.cn/release/download_crawler_static/12341539/bg11.jpg)
For example, when saving a document in Microsoft Word, using a version before 2003, the fi le cre-
ated (which has a doc extension) is in a binary format. If you open the fi le in a text editor such as
Notepad, you won’t be able to see a picture of the original Word document; the best you’ll be able to
see is the occasional line of text surrounded by gibberish rather than the prose, which could be in a
number of formats such as bold or italic. The characters in the document other than the actual text
are metadata, literally information about information. Mixing data and metadata is both common
and straightforward in a binary fi le. Metadata can specify things such as which words should be
shown in bold, what text is to be displayed in a table, and so on. To interpret this fi le you the need
the help of the application that created it. Without the help of a converter that has in-depth knowl-
edge of the underlying binary format, you won’t be able to open a document created in Word with
another similar application such as WordPerfect. The main advantage of binary formats is that they
are concise and can be expressed in a relatively small space. This means that more fi les can be stored
(on a hard drive, for example) but, more importantly nowadays, less bandwidth is used when trans-
porting these fi les across networks.
Text Files
The main difference between text and binary fi les is that text fi les are human and machine readable.
Instead of a proprietary format that needs a specifi c application to decipher it, the data is such that
each group of bits represents a character from a known set. This means that many different applica-
tions can read text fi les. On a standard Windows machine you have a choice of Notepad, WordPad,
and others, including being able to use command-line–based utilities such as Edit. Non-Windows
machines have a similar wide range insert of programs available, such as Emacs and Vim.
NOTE The way that characters are represented by the underlying data stream
is referred to as a fi le’s encoding. The specifi c encoding used is often present
as the fi rst few bytes in the fi le; an application checks these bytes upon opening
the fi le and then knows how to display and manipulate the data. There is also a
default encoding if these fi rst few bytes are not present. XML also has other
ways of specifying how a fi le was encoded, and you’ll see these later on.
The ability to be read and understood by both humans and machines is not the only advantage
of text fi les; they are also comparatively easier to parse than binary fi les. The main disadvantage
however, is their size. In order for text fi les to contain metadata (for example, a stretch of text to be
marked as important), the relevant words are usually surrounded by characters denoting this extra
information, which are somehow differentiated from the actual text itself. The most common exam-
ples of this can be found in HTML, where angle brackets are special symbols used to convey the
meaning that anything within them refers to how the text should be treated rather than the actual
data. For example, if I want mark a phrase as important I can wrap it like so:
<strong>returns must include the item order number</strong>
Steps Leading up to XML: Data Representation and Markups
❘
5
![](https://csdnimg.cn/release/download_crawler_static/12341539/bg12.jpg)
6
❘
CHAPTER 1 WHAT IS XML?
Another disadvantage of text fi les is their lack of support for metadata. If you open a Word docu-
ment that contains text in an array of fonts with different styles and save it as a text fi le, you’ll just
get a plain rendition; all of the metadata has been lost. What people were looking for was some way
to have the best of both worlds — a human-readable fi le that could also be read by a wide range
of applications, and could carry metadata along with its content. This brings us to the subject of
markup.
A Brief History of Markup
The advantages of text fi les made it the preferred choice over binary fi les, yet the disadvantages were
still cumbersome enough that people wanted to also standardize how metadata could be added.
Most agreed that markup, the act of surrounding text that conveyed information about the text,
was the way forward, but even with this agreed there was still much to be decided. The main two
questions were:
➤ How can metadata be differentiated from the basic text?
➤ What metadata is allowed?
For example, some documents needed the ability to mark text as bold or italic whereas others
were more concerned with who the original document author was, when was it created, and
who had subsequently modifi ed it. To cope with this problem a defi nition called Standard
Generalized Markup Language was released, commonly shortened to SGML. SGML is a step
removed from defi ning an actual markup language, such as the Hyper Text Markup Language,
or HTML. Instead it relays how markup languages are to be defi ned. SGML allows you to
create your own markup language and then defi ne it using a standard syntax such that any
SGML-aware application can consume documents written in that language and handle them
accordingly. As previously noted, the most ubiquitous example of this is HTML. HTML uses
angular brackets (< and >) to separate metadata from basic text and also defi nes a list of
what can go into these brackets, such as
em for emphasizing text, tr for table, and td for
representing tabular data.
THE BIRTH OF XML
SGML, although well thought-out and capable of defi ning many different types of markup, suffered
from one major failing: it was very complicated. All the fl exibility came at a cost, and there were
still relatively few applications that could read the SGML defi nition of a markup language and use
it to correctly process documents. The concept was correct, but it needed to be simpler. With this
goal in mind, a small working group and a larger number of interested parties began working in the
mid-1990s on a subset of SGML known as Extensible Markup Language (XML). The fi rst working
draft was published in 1996 and two years later the W3C published a revised version as a recom-
mendation on February 10, 1998.
![](https://csdnimg.cn/release/download_crawler_static/12341539/bg13.jpg)
NOTE The World Wide Web Consortium (W3C) is the main international stan-
dards organization for the World Wide Web. It has a number of working groups
targeting di erent aspects of the Web that discuss standardization and docu-
mentation of the di erent technologies used on the Internet. The standards doc-
uments go through various stages such as Working Draft and Candidate
Recommendation before fi nally becoming a Recommendation. This process can
take many years. The reason that the fi nal agreement is called a recommenda-
tion rather than a standard is that you are still free to ignore what it says and use
your own. All web developers know the problems in developing applications that
work across all browsers, and many of these problems arise because the
browser vendors did not follow a W3C recommendation or they did not imple-
ment features before the recommendation was fi nalized. Most of the XML tech-
nologies discussed in this book have a W3C recommendation associated with
them, although some don’t have a full recommendation because they are still in
draft form. Additionally, some XML-related standards originate from outside the
W3C, such as SAX which is discussed in Chapter 11, “Event Driven Programming.”
and therefore they also don’t have o cial W3C recommendations.
XML therefore derived as a subset of SGML, whereas HTML is an application of SGML. XML
doesn’t dictate the overall format of a fi le or what metadata can be added, it just specifi es a few
rules. That means it retains a lot of the fl exibility of SGML without most of the complexity. For
example, suppose you have a standard text fi le containing a list of application users:
Joe Fawcett
Danny Ayers
Catherine Middleton
This fi le has no metadata; the only reason you know it’s a list of people is your own knowledge and
experience of how names are typically represented in the western world. Now look at these names
as they might appear in an XML document:
<applicationUsers>
<user firstName=”Joe” lastName=”Fawcett” />
<user firstName=”Danny” lastName=”Ayers” />
<user firstName=”Catherine” lastName=”Middleton” />
</applicationUsers>
Immediately it’s more apparent what the individual pieces of data are, although an application still
wouldn’t know just from that fi le how to treat a
user or what firstName means. Using the XML
format rather than the plain text version, it’s much easier to map these data items within the appli-
cation itself so they can be handled correctly.
The two common features of virtually all XML fi le are called elements and attributes. In the pre-
ceding example, the elements are
applicationUsers and user, and the attributes are firstName
and
lastName.
The Birth of XML
❘
7
![](https://csdnimg.cn/release/download_crawler_static/12341539/bg14.jpg)
8
❘
CHAPTER 1 WHAT IS XML?
A big disadvantage of this metadata, however, is the consequent increase in the size of the fi le. The
metadata adds about 130 extra characters to the fi le’s original 43 character size, an increase of more
than 300 percent. The creators of XML decided that the power of metadata warranted this increase
and, indeed, one of their maxims during the design was that terseness is not an aim, a decision that
many would later come to regret.
NOTE Later on in the book you’ll see a number of ways to minimize the size of
an XML fi le if needed. However, all these methods are, to some extent, a trade-
o against readability and ease of use.
Following is a simple exercise to demonstrate the differences in how applications handle simple text
fi les against how XML is treated. Even though the application, in this case a browser, is told noth-
ing in advance of opening the two fi les, you’ll see how much more metadata is available in the XML
version compared to the text one.
TRY IT OUT Opening an XML File in a Browser
This example shows the differences in how XML fi les can be handled compared to plain text fi les.
1. Create a new text fi le in Notepad, or an equivalent simple text editor, and paste in the list of
names fi rst shown earlier.
2. Save this fi le at a convenient location as appUsers.txt.
3. Next, open a browser and paste the path to appUsers.txt into the address bar. You should see
something like Figure 1-1. Notice how it’s just a simple list:
FIGURE 11
4. Now create another text fi le based on the XML version and save it as appUsers.xml. If you’re
doing this in Notepad make sure you put quotes around the full name before saving or otherwise
you’ll get an unwanted
.txt extension added.
5. Open this fi le and you should see something like Figure 1-2.
剩余821页未读,继续阅读
![xml](https://img-home.csdnimg.cn/images/20210720083646.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://profile-avatar.csdnimg.cn/374352021295426586961eddfc01f08f_qq_30203441.jpg!1)
qq_30203441
- 粉丝: 0
- 资源: 4
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![](https://csdnimg.cn/release/wenkucmsfe/public/img/voice.245cc511.png)
最新资源
- Vue实现iOS原生Picker组件:详细解析与实现思路
- Arduino蓝牙小车:参数调试与功能控制
- 百度Java面试精华:200页精选资源涵盖核心知识点
- Swift使用CoreData填坑指南:CoreData在Swift 3.0的变化
- 微距离无线充电器创新设计及其实验探索
- MTK Android平台开发全攻略:44步详解流程
- RecyclerView全面解析:替代ListView的新选择
- Android开发:自动适配中英文键盘解决方案
- Android调用WebService接口教程
- Android开发:BitmapUtil图片处理全解析与实例
- Android多线程断点续传实现详解
- PCA算法在人脸识别会议签到系统中的应用
- EventBus 3.0:Android事件总线详解与实战应用
- Android FileUtil:全面解析文件操作实用技巧与实例
- RecyclerView添加头部和尾部实战教程
- Android实现微博滑动固定顶部栏实战与优化
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![](https://img-home.csdnimg.cn/images/20220527035711.png)
![](https://img-home.csdnimg.cn/images/20220527035711.png)
![](https://img-home.csdnimg.cn/images/20220527035111.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![](https://csdnimg.cn/release/wenkucmsfe/public/img/green-success.6a4acb44.png)