?
Reading XML using the XML guide in C #
?
Two types of models are accessed:
?
Access to the program in order to manipulate the XML file generally has two models, namely the use of the DOM (Document Object model) and flow model, the advantage of using the DOM is that it allows the editing and updating of XML documents, can randomly access the data in the document, you can use the XPath query, but, The disadvantage of DOM is that it requires a one-time load of the entire document into memory, which can cause resource problems for large documents. The flow model solves this problem very well because its access to the XML file is based on the concept of flow, that is, at any time in memory only the current node, but it also has its shortcomings, it is read-only, forward-only, not in the document to perform a backward navigation operation. Although it is a difference, but we can also be in the program and the implementation of the pros and cons, oh, this is an off-topic! We are talking about XML reading today, so let's discuss the flow model in detail.
?
?
Variants in the flow model:
Each time the flow model iterates over a node in an XML document, it is suitable for processing large documents with little memory space. There are two variants in the flow model-the "push" model and the "pull" model.
Push model is often said Sax,sax is an event-driven model, that is to say: Every node it discovers a push model to trigger an event, and we have to write the handlers of these events, which is very inflexible and cumbersome.
. NET is based on the implementation of the "pull" model, the "pull" model when traversing the document will be interested in the document part from the reader, do not need to raise events, allowing us to programmatically access the document, which greatly improves the flexibility, the performance of the "pull" model can be selective processing node, While Sax notifies the client of every node it discovers, using a "pull" model can improve the overall efficiency of the application. The "pull" model in. NET is implemented as a XmlReader class, and the following is a look at the inheritance structure of the class:
?
Let's talk about it today. The XmlTextReader class in the architecture, which provides the ability to read an XML file, which verifies that the document is well-formed and that, if it is not a well-formed XML document, the class throws a XmlException exception during the read. You can use some of the methods provided by this class to read the document node, filtering and so on, and get the name and value of the node, keep in mind: XmlTextReader is based on the implementation of the flow model, an inappropriate analogy, the XML file as if the water source, the gate of a boiling water outflow, through the flow will not be able to flow back. At any time in memory only the current node, you can use the Read () method of the XmlTextReader class to read the next node. Well, say so many to see an example, programming to pay attention to the actual, right. See the code before you look at the effect of the operation!
?
?
Here I'll cover three common ways to read XML files. respectively is
Using XmlDocument
Using XmlTextReader
Using Linq to XML
?
Here I first create an XML file, named Book.xml all of the following methods are based on this XML file, the file content is as follows:
?
1: <?xml version="1.0" encoding="utf-8"?>
2: <bookstore>
3: <!--记录书本的信息-->
4: <book Type="必修课" ISBN="7-111-19149-2">
5: <title>数据结构</title>
6: <author>严蔚敏</author>
7: <price>30.00</price>
8: </book>
9: <book Type="必修课" ISBN="7-111-19149-3">
10: <title>路由型与交换型互联网基础</title>
11: <author>程庆梅</author>
12: <price>27.00</price>
13: </book>
14: <book Type="必修课" ISBN="7-111-19149-4">
15: <title>计算机硬件技术基础</title>
16: <author>李继灿</author>
17: <price>25.00</price>
18: </book>
19: <book Type="必修课" ISBN="7-111-19149-5">
20: <title>软件质量保证与管理</title>
21: <author>朱少民</author>
22: <price>39.00</price>
23: </book>
24: <book Type="必修课" ISBN="7-111-19149-6">
25: <title>算法设计与分析</title>
26: <author>王红梅</author>
27: <price>23.00</price>
28: </book>
29: <book Type="选修课" ISBN="7-111-19149-1">
30: <title>计算机操作系统</title>
31: <author>7-111-19149-1</author>
32: <price>28</price>
33: </book>
34: </bookstore>
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
Reading XML using the XML guide in C #