XML reader and writer -- read XML from stream

Source: Internet
Author: User
Tags xml reader
How to read XML data from a stream

This example illustrates how to use the xmltextreader class to read XML from a stream. Streams may come from different sources, such as byte streams from servers, files, or textreader.

Note: This example shows how to continue reading the XML topic from a file.

 

VB readxmlstream. aspx

[Running example] | [View Source code]

Xmltextreader has different constructors to specify the location of XML data. In this example, xmltextreader is loaded from the stream. A stream is an abstract representation of the input or output device of the source or target of data (XML data here. You can write or read streams. The most visualized representation of a stream is a byte stream. The stream can provide independence independent from the device, so if the stream source changes, it does not requireProgramAlso make changes.

ExampleCodeCreate a stringreader class that can constitute an XML string. Because the string is a pure byte stream retained in the memory, you can use xmltextreader to analyze the word throttling as XML. This memory stream has no specific encoding. In this example, create an xmltextreader to analyze the stream and display the obtained XML.


Stringreader stream; xmltextreader reader = NULL; try {console. writeline ("Initializing stringreader..."); stream = new stringreader ("<? XML version = '1. 0'?> "+" <! -- This file represents a fragment of a book store inventory database --> "+" <bookstore> "+" <book genre = \ "autobiography \" publicationdate = \ "1981 \" ISBN = \ "1-861003-11-0 \"> "+" <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 genre = \ "novel \ "Publicationdate = \" 1967 \ "ISBN = \" 0-201-63361-2 \ ">" + "<title> the confidence man </title>" + "<author>" + "<first -Name> Herman </first-name> "+" <last-name> Melville </last-name> "+" </author> "+" <price> 11.99 </price> "+" </book> "+" <book genre = \ "philosophy \" publicationdate = \ "1991 \" ISBN = \ "1-861001-57-6 \"> "+" <title> the gorgias </title> "+" <author> "+" <Name> Plato </Name> "+" </author> "+ "<Price> 9.99 </price>" + "</book>" + "</bookstore> "); // load the xmltextreader from the stream reader = new xmltextreader (Stream); console. writeline ("processing... "); console. writeline (); formatxml (Reader);} catch (exception e) {console. writeline ("exception: {0}", E. tostring ();} finally {console. writeline (); console. writeline ("processing of stream complete. "); // finished with xmltext Reader if (reader! = NULL) reader. Close ();}
Dim stream as stringreader dim reader as xmltextreader = nothing try console. writeline ("Initializing stringreader...") stream = new stringreader ("<? XML version = '1. 0'?> "& _" <! -- This file represents a fragment of a book store inventory database --> "& _" <bookstore> "& _" <book genre = "" autobiography "" publicationdate = "" 1981" "ISBN =" "1-861003-11-0"> "& _" <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 genre =" "novel" "publicationdate =" "1967" "ISBN = "" 0-201-63361-2 ""> "& _" <title> the confidence man </title> "& _" <author> "& _" <first- name> Herman </first-name> "& _" <last-name> Melville </last-name> "& _" </author> "& _" <price> 11.99 </price> "& _" </book> "& _" <book genre = "" Philosophy "" publicationdate = "" 1991 "" ISBN = "" 1-861001 -57-6 ""> "& _" <title> the gorgias </title> "& _" <author> "& _" <Name> Plato </Name>" & _ "</author>" & _" <price> 9.99 </price> "& _" </book> "& _" </bookstore> ") 'Load the xmltextreader from the stream reader = new xmltextreader (Stream) console. writeline ("processing... ") console. writeline () formatxml (Reader) catch e as exception console. writeline ("exception: {0}", E. tostring () Finally console. writeline () console. writeline ("processing of stream complete. ") if not reader is nothing reader. close () end if end try
C # VB  

If the stream is provided as an input, the xmltextreader attribute decodes the stream by including it in streamreader and then calling the switchencoding Attribute Based on the specified XML encoding. In addition, xmlresolver is used to parse the external resources required for correct analysis input, such as document type definition (DTD) and architecture. Another method for stream reading is to use the streamreader class, which can be used to read the reader of the upstream stream. The following code example loads a file named books. xml and uses xmltextreader to analyze the result file.

 streamreader = new streamreader ("books. XML "); console. writeline ("file books. XML read sucessfully... "); // load the xmltextreader from the streamreader xmltextreader xmlreader = new xmltextreader (streamreader); 
 dim streamreader as streamreader = new streamreader (" books. XML ") console. writeline ("file books. XML read sucessfully... ") 'Load the xmltextreader from the streamreader dim xmlreader as xmltextreader = new xmltextreader (streamreader) 
C # VB

The formalxml method in this example shows how to use the movetonextattribute method to move an element node to an attribute node when the current node is an element node. This allows you to not only access the name and value attributes of the node, but also obtain certain attributes, such as the current namespace of the attribute, because it is in the context of the node. The following code example also shows the format method, which displays the name and value of the current node.

Private Static void formatxml (xmlreader reader) {int picount = 0, doccount = 0, commentcount = 0, elementcount = 0, attributecount = 0, textcount = 0, whitespacecount = 0; while (reader. read () {Switch (reader. nodetype) {Case xmlnodetype. processinginstruction: Format (reader, "processinginstruction"); picount ++; break; Case xmlnodetype. documenttype: Format (reader, "documenttype"); doccount ++; break; Case xmlnodetype. comment: Format (reader, "comment"); commentcount ++; break; Case xmlnodetype. element: Format (reader, "element"); While (reader. movetonextattribute () {format (reader, "attribute");} elementcount ++; If (reader. hasattributes) attributecount + = reader. attributecount; break; Case xmlnodetype. text: Format (reader, "text"); textcount ++; break; Case xmlnodetype. whitespace: whitespacecount ++; break ;}// display the statistics console. writeline (); console. writeline ("Statistics for stream"); console. writeline (); console. writeline ("processinginstruction: {0}", picount ++); console. writeline ("documenttype: {0}", doccount ++); console. writeline ("comment: {0}", commentcount ++); console. writeline ("element: {0}", elementcount ++); console. writeline ("attribute: {0}", attributecount ++); console. writeline ("text: {0}", textcount ++); console. writeline ("whitespace: {0}", whitespacecount ++);} // format the output Private Static void format (xmlreader reader, string nodetype) {// format the output console. write (reader. depth + ""); console. write (reader. attributecount + ""); For (INT I = 0; I <reader. depth; I ++) {console. write ('\ t');} console. write (nodetype + "<" + reader. name + ">" + reader. value); console. writeline ();}
Private shared sub formatxml (reader as xmltextreader) dim picount, doccount, commentcount, elementcount as integer dim attributecount, textcount, whitespacecount as integer while reader. read () Select (reader. nodetype) Case xmlnodetype. processinginstruction: Format (reader, "processinginstruction") picount + = 1 case xmlnodetype. documenttype: Format (reader, "documenttype") doccount + = 1 case xmlnodetype. comment: Format (reader, "comment") commentcount + = 1 case xmlnodetype. element: Format (reader, "element") elementcount + = 1 while reader. movetonextattribute () format (reader, "attribute") end while if (reader. hasattributes) attributecount + = reader. attributecount end if case xmlnodetype. text: Format (reader, "text") textcount + = 1 case xmlnodetype. whitespace: whitespacecount + = 1 end select end while 'display the statistics for the file console. writeline () console. writeline ("Statistics for stream") console. writeline () console. writeline ("processinginstruction:" & picount) console. writeline ("documenttype:" & doccount) console. writeline ("comment:" & commentcount) console. writeline ("element:" & elementcount) console. writeline ("attribute:" & attributecount) console. writeline ("text:" & textcount) console. writeline ("whitespace:" & whitespacecount) end sub private shared sub format (byref reader as xmltextreader, nodetype as string) 'format the output console. write (reader. depth & "") console. write (reader. attributecount & "") dim I as integer for I = 0 to reader. depth console. write (strings. CHR (9) Next console. write (reader. prefix & nodetype & "<" & reader. name & ">" & reader. value) console. writeline () end sub
C # VB  
Summary
    1. A stream is an abstract representation of the input or output device as the source or target of the data.
    2. Xmltextreader provides constructors for reading XML from strings indicating URLs, or reading XML from local file names, streams, or textreader.
    3. Xmltextreader provides constructors for reading XML from file names, streams, or textreader.
    4. You can use the movetonextattribute method to access attribute nodes. This method allows you to determine the attributes of attribute nodes.

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.