Easy processing of XML data in the. NET Framework (1-2)
Source: Internet
Author: User
? XmlReader class
?? The XML browser supports a programming interface that is used to connect XML documents and "roll out" the data you want. If you go deeper into the browser, you will invent the principle that the browser works similar to how our desktop uses the program to extract data from the database. The database service returns a cursor object that contains all the query result sets and returns a reference to the beginning address of the target dataset. The client of the XML browser receives a reference to the browser instance. The instance extracts the underlying data stream and emerges the extracted data as an XML tree. The browser class provides a read-only, forward-only cursor, and you can rotate the cursor through each piece of data in the result set by using the browser class feed method.
?? From the browser, the XML document is not a label text file, but rather a serialized node aggregation. It is a special cursor pattern in the. NET Framework and you cannot find any other similar API functions in the. NET Framework.
?? There are several different places for browsers and XMLDOM analyzers. The XML browser is a forward-only, it has no concept of parent, child, ancestor, sibling node, and is read-only. In the. NET framework, reading and writing XML documents is divided into two completely different functions, distinguished by the XmlReader and XmlWriter classes. To compile an XML document, you can use the XMLDOM parser, or you could design a class yourself to achieve both of these effects. Let's start by analyzing the browser's program efficacy.
?? XmlReader is an abstract class that you can continue and expand its effectiveness. User programs are generally based on the following three categories: XmlTextReader, XmlValidatingReader, or XmlNodeReader. All of these classes have a property and a method of figure two. It is worth noticing that the values of some properties are actually attached to one of the actual browser classes, and different classes may differ from the base class. Therefore, the clarification of each attribute in figure A is based on the base class. For example, the Canresolveentity property returns only true in the XmlValidatingReader class, but it can be set to false in other browser classes. Similarly, the actual return value of some of the methods in figure two may be different for different classes. For example, if the node type is not an element node, all the return value types of the method containing the atrributes are void.
?? The XmlTextReader class uses a forward-only, read-only method to quickly visit an XML data stream. The browser first verifies that the XML document is well-formed and throws an exception if it is not. XmlTextReader checks if the DTD is well-formed, but does not apply a DTD to validate the document. XmlTextReader uses the file name of an XML document, or its URL, or loads an XML document from a file stream, and then quickly processes the XML document data. If you need to validate the data in the document, you can use the XmlValidatingReader class.
?? You can create instances of the XmlTextReader class in a variety of ways, load files from a hard disk, load from a URL address, stream (streams), and read from text into XML document data:
?? XmlTextReader reader = new XmlTextReader (file);
?? Keep in mind that all public structure functions of the XmlTextReader class ask you to specify a data source, which can be a stream, a file, or something else. XmlTextReader the default structure function is protected (protected), so it cannot be applied directly. Like all browser classes in the. NET framework (such as the SqlDataReader Class), once the browser object is connected and opened, you can use the Read method to visit the data. At the beginning, you can only move the pointer to the first element using the Read method, and then we can move the pointer to the next node element using the Read method or other methods such as Skip, movetocontent, and ReadInnerXml. To process the contents of all XML documents, you can iterate through the contents of the document with a loop based on the return value of the Read method, because the Read method returns a Boolean value that, when read to the tail node of the document, returns FALSE, otherwise it returns true.
?? Figure 3 outputting an XML Document Node Layout
?? String getxmlfilenodelayout (String file)
?? {
?? Create a XmlTextReader class to point it to the target XML document
?? XmlTextReader reader = new XmlTextReader (file);
?? Loops out the text of the node and puts it into the StringWriter object instance
?? StringWriter writer = new StringWriter ();
?? string tabprefix = ';
?? while (reader. Read ())
?? {
?? Write-start tag, if the node type is an element
?? if (reader. NodeType = = XmlNodeType.Element)
?? {
?? Participates in reader based on the depth of the node where the element is located. Depth the tab character and write the element name into <>.
?? Tabprefix = new string (' \ t ', reader. Depth);
?? Writer. WriteLine (' {0}<{1}> ', Tabprefix, reader. Name);
??}
?? Else
?? {
?? Write end tag, if the node type is an element
?? if (reader. NodeType = = xmlnodetype.endelement)
?? {
?? Tabprefix = new string (' \ t ', reader. Depth);
?? Writer. WriteLine (' {0} ', Tabprefix, reader. Name);
??}
??}
??}
?? Output to screen
?? String buf = writer. ToString ();
?? Writer. Close ();
?? Closed flow
?? Reader. Close ();
?? return buf;
??}
?? Figure III illustrates a shorthand function for outputting a node element of a given XML document. The function opens an XML document and then loops through all the contents of the XML document. Each time the Read method is called, the browser pointer moves down one node. In most cases, element nodes can be processed with the Read method, but sometimes when you move from one node to the next, you may move between two different types of nodes. However, the Read method cannot be moved between attribute nodes. The MoveToContent method of the browser allows the pointer to jump from the head node position to the first content node status. The Skip method can also be used to move pointers in ProcessingInstruction, DocumentType, Comment, whitespace, and significantwhitespace type nodes.
The above is the easy processing of XML data (1-2) in the. NET framework, and more about topic.alibabacloud.com (www.php.cn)!
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.