home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 40 / IOPROG_40.ISO / SOFT / NETFrameworkSDK.exe / comsdk.cab / samples.exe / QuickStart / howto / doc / XML / ReadXMLStream.aspx < prev    next >
Encoding:
Text File  |  2000-06-10  |  6.9 KB  |  183 lines

  1. <%@ Register TagPrefix="Acme" TagName="SourceRef" Src="/quickstart/util/SrcRef.aspx"%>
  2.  
  3. <!-- #include virtual="/quickstart/howto/doc/xml/header.inc" -->
  4.  
  5. <h4>How Do I...Read XML data from a stream?</h4>
  6.  
  7. <div class="indent" style="width:660">
  8. This sample illustrates how to read XML from a stream using the XmlTextReader class. The stream could have come from a variety of sources such as a byte stream
  9. from a server, a file or from a TextReader. <br clear="left"><br>
  10. This sample follows on from the <a href="/quickstart/howto/doc/Xml/ReadXmlFile.aspx">How do I...Read XML from a file?</a> topic.
  11. </div>
  12.  
  13. <h4>Reading XML from a stream</h4> 
  14. <Acme:SourceRef
  15. ViewSource="/quickstart/howto/samples/Xml/ReadXmlStream/ReadXmlStream.src"
  16. RunSample="/quickstart/howto/samples/Xml/ReadXmlStream/ReadXmlStream.aspx" 
  17. Icon = "/quickstart/images/genicon.gif"
  18. Caption="ReadXmlStream.aspx"
  19. runat="server" />
  20.  
  21. <br clear="left"><br>
  22. <div class="indent" style="width:660">
  23. The XmlTextReader has different constructors to specify the location of the XML data. Here we are going to load the XmlTextReader from a stream.
  24. A stream is an abstract representation of an input or output device that is the source of, or destination for, data (in this case XML data). You can write to 
  25. a stream and read from a stream, which is best visualised as a flow of bytes. Streams are used to provide independence from the device and hence require
  26. no program changes if, for instance, the source of a stream changes.
  27.  
  28. <br clear="left"><br>
  29. <div style="width:660">
  30. The sample code shown below creates a StringReader class which is then used to build up an XML string. Since this is purely a byte stream which is held in memory,
  31. we can get the XmlTextReader to parse this stream as XML. This in memory stream has no particular specified encoding. An XmlTextReader is then created which parses
  32. the stream and displays the resultant XML.
  33.  
  34. <div class="code"><xmp>
  35. StringReader stream;
  36. XmlTextReader reader = null;
  37.  
  38. stream = new StringReader("<?xml version='1.0'?>" +
  39.                           "<!-- This file represents a fragment of a book store inventory database -->" +
  40.                           "<bookstore>" +
  41.                           " <book genre=\"autobiography\" publicationdate=\"1981\" ISBN=\"1-861003-11-0\">" +
  42.                           "   <title>The Autobiography of Benjamin Franklin</title>" +
  43.                           "   <author>" +
  44.                           "       <first-name>Benjamin</first-name>" +
  45.                           "       <last-name>Franklin</last-name>" +
  46.                           "   </author>" +
  47.                           "   <price>8.99</price>" +
  48.                           " </book>" +
  49.                           " <book genre=\"novel\" publicationdate=\"1967\" ISBN=\"0-201-63361-2\">" +
  50.                           "   <title>The Confidence Man</title>" +
  51.                           "   <author>" +
  52.                           "       <first-name>Herman</first-name>" +
  53.                           "       <last-name>Melville</last-name>" +
  54.                           "   </author>" +
  55.                           "   <price>11.99</price>" +
  56.                           " </book>" +
  57.                           "  <book genre=\"philosophy\" publicationdate=\"1991\" ISBN=\"1-861001-57-6\">" +
  58.                           "   <title>The Gorgias</title>" +
  59.                           "   <author>" +
  60.                           "       <name>Plato</name>" +
  61.                           "   </author>" +
  62.                           "   <price>9.99</price>" +
  63.                           " </book>" +
  64.                           "</bookstore>");
  65.  
  66. // Load the XmlTextReader from the stream
  67. reader = new XmlTextReader (stream);
  68.  
  69. Console.WriteLine ("Processing ...");
  70. Console.WriteLine ();
  71. FormatXml(reader);
  72.  
  73. // Finished with XmlTextReader
  74. if (reader != null)
  75.     reader.Close();
  76. </xmp></div>
  77.  
  78. <div style="width:660">
  79. If a Stream is supplied as input, the XmlTextReader properly decodes this by wrapping the stream in a StreamReader and calling the SwitchEncoding property according 
  80. to the XML encoding specified. Also the XmlResolver is used to resolve external resources needed to correctly parse the input - like DTD's, schemas, entities
  81. and <xml:include>s. Another method of representing a stream is by using a StreamReader class that implements a reader which reads from a character stream.
  82. The code sample below illustrates loading a file named books.xml and then using the XmlTextReader to parse the resulting file.</div>
  83.  
  84. <div class="code"><xmp>
  85. StreamReader streamreader = new StreamReader ("books.xml");
  86. Console.WriteLine ("File books.xml read sucessfully ...");
  87.  
  88. // Load the XmlTextReader from the StreamReader
  89. XmlTextReader xmlreader = new XmlTextReader (streamreader);
  90. </xmp></div>
  91.  
  92. <div style="width:660">
  93. This FormalXml method in this sample shows how to move to the attribute nodes using the <b>MoveToNextAttribute</b> method when the current node is an element
  94. node. This allows you not only to access the node's name and value properties but, because you are at a node context you can also get properties like the current
  95. namespace for the attribute. The code sample below also shows the Format method that this time displays the name and value for the current node.</div>
  96.  
  97. <div class="code"><xmp>
  98. private static void FormatXml (XmlReader reader)
  99. {
  100.     int PI_count=0, Doc_count=0, comment_count=0, element_count=0, attribute_count=0, text_count=0, whitespace_count=0;
  101.  
  102.     while (reader.Read())
  103.     {
  104.         switch (reader.NodeType)
  105.         {
  106.         case XmlNodeType.Element:
  107.             Format (reader, "Element");
  108.             while(reader.MoveToNextAttribute())
  109.             {
  110.                 Format (reader, "Attribute");
  111.             }
  112.             element_count++;
  113.             if (reader.HasAttributes)
  114.                 attribute_count += reader.AttributeCount;
  115.             break;
  116.          }
  117.     }
  118. }
  119.  
  120. // Format the output
  121. private static void Format(XmlReader reader, String NodeType)
  122. {
  123.     // Format the output
  124.     Console.Write(reader.Depth + " ");
  125.     Console.Write(reader.AttributeCount + " ");
  126.     for (int i=0; i < reader.Depth; i++)
  127.     { 
  128.         Console.Write('\t');
  129.     }
  130.  
  131.     Console.Write(NodeType + "<" + reader.Name + ">" + reader.Value);
  132.     Console.WriteLine();
  133. }
  134. </xmp></div>
  135.  
  136. <H4>Summary</H4>
  137. <OL>
  138. <LI>A stream is an abstract representation of an input or output device that is the source of, or destination for, data.
  139. <LI>The XmlTextReader provides constructors to read XML from a file, a stream or a TextReader.
  140. <LI>The XmlTextReader properly decodes the stream by wrapping the stream in a StreamReader and setting the specified XML encoding.
  141. <LI>Attribute nodes can be accessed with the MoveToNextAttribute method that allows you to determine the properties of the attribute node.
  142. </OL>
  143.  
  144. <!-- #include virtual="/quickstart/howto/include/footer.inc" -->
  145. </xml>
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.