home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 40 / IOPROG_40.ISO / SOFT / NETFrameworkSDK.exe / comsdk.cab / samples.exe / QuickStart / howto / doc / XML / ReadXMLFile.aspx < prev    next >
Encoding:
Text File  |  2000-06-10  |  13.1 KB  |  378 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 from a file?</h4>
  6.  
  7. <div class="indent" style="width:660">
  8. This sample illustrates how to read XML from a file using the XmlTextReader class.
  9. This class provides direct parsing and tokenizing of XML and implements the <A href="http://www.w3.org/TR/1998/REC-xml-19980210">W3C 
  10. Extensible Markup Language (XML) 1.0</A> and the <A><A href="http://www.w3.org/TR/REC-xml-names/">Namespaces in XML</A></A> specifications.
  11. This reader provides fast, tokenized, stream access to XML rather than using an object model such as the XML DOM.
  12. See <A target=content href="DOMInterfaceXmlDocument.aspx">How Do I...Create and use the XmlDocument (W3C DOM)?</a></div>
  13.  
  14. <br clear="left"><div class="indent" style="width:660">
  15. The XmlReader class is the API which provides the XML parsing. The XmlTextReader is an implementation of this API to handle byte streams.
  16. </div>
  17.  
  18. <h4>Reading XML from a file</h4> 
  19. <Acme:SourceRef 
  20. ViewSource="/quickstart/howto/samples/Xml/ReadXmlFile/ReadXmlFile.src"
  21. RunSample="/quickstart/howto/samples/Xml/ReadXmlFile/ReadXmlFile.aspx" 
  22. Icon = "/quickstart/images/genicon.gif"
  23. Caption="ReadXmlFile.aspx"
  24. runat="server" />
  25.  
  26. <br clear="left"><br><div class="indent" style="width:660">
  27. Typically the XmlTextReader is used if you need to access the XML as 'raw' data without the overhead of a DOM and therefore provides a faster 
  28. mechanism for reading XML.  For example an XML document could have a header section used for routing the document for processing elsewhere.
  29. The XmlTextReader has different constructors to specify the location of the XML data. In this sample we are going to load XML from the
  30. <a target="_blank" href="/quickstart/util/srcctrlwin.aspx?path=/quickstart/howto/samples/Xml/ReadXmlFile/&file=books.xml">books.xml</a> file.
  31. The sample code shown below constructs an XmlTextReader.
  32.  
  33. <div class="code"><xmp>
  34. XmlTextReader reader = new XmlTextReader ("books.xml");
  35. </xmp></div>
  36.  
  37. <div style="width:660">
  38. Once loaded, the XmlTextReader moves across the XML data by performing sequential reads to get the next record using the <b>Read</b> method. It returns false
  39. if there are no more records.
  40. <div class="code">
  41. <xmp>
  42. while (reader.Read())
  43. {
  44.     // Do some work here on the data
  45. }
  46. </xmp></div>
  47.  
  48. <div style="width:660">
  49. To processes the XML data, each record has a node type which can be determined from the <b>NodeType</b> property. The <b>Name</b> and <b>Value</b> properties return
  50. the node name (e.g. the element and attribute names) and the node value (i.e. the node text) of the current node (or record). The code sample below uses these
  51. properties to display the details about the node for Element and DocumentType types. The node type is determined by the NodeType enumeration shown
  52. in the table.
  53.  
  54. <div class="code"><xmp>
  55. while (reader.Read())
  56. {
  57.     switch (reader.NodeType)
  58.     {
  59.     case XmlNodeType.Element: // The node is an Element
  60.         Console.WriteLine(NodeType + "<" + reader.Name + ">" + reader.Value);
  61.         break;
  62.     case XmlNodeType.DocumentType: // The node is a DocumentType
  63.         Console.WriteLine(NodeType + "<" + reader.Name + ">" + reader.Value);
  64.         break;
  65.     }
  66. }
  67. </xmp></div>
  68.  
  69. <div>Table of NodeTypes which are equivalent to the W3C DOM node types with some extended types required for forward only reading.</div><br clear="left"><br>
  70.  
  71. <DIV class=indent>
  72. <TABLE class=table 
  73. style="border-style: solid"
  74. width="418">
  75. <TBODY>
  76. <TR>
  77. <TH width="100">NodeType Enum</TH>
  78. <TH width="308">Description</TH>
  79. <TH width="10">Value</TH>
  80. </TR>
  81.   <tr>
  82.     <td height="17"><font size="1">None</font></td>
  83.     <td height="17"><font size="1"></font></td>
  84.     <td height="17"><font size="1">0</font></td>
  85.   </tr>
  86.   <tr>
  87.     <td width="100" height="19"><font size="1">Element</font></td>
  88.     <td width="308" height="19"><font size="1"><name></font></td>
  89.     <td width="10" height="17"><font size="1">1</font></td>
  90.   </tr>
  91.   <tr>
  92.     <td width="100" height="19"><font size="1">Attribute</font></td>
  93.     <td width="308" height="19"><font size="1">id='123'</font></td>
  94.     <td width="10" height="17"><font size="1">2</font></td>
  95.   </tr>
  96.   <tr>
  97.     <td width="100" height="19"><font size="1">Text</font></td>
  98.     <td width="308" height="19"><font size="1">'123'</font></td>
  99.     <td width="10" height="17"><font size="1">3</font></td>
  100.   </tr>
  101.   <tr>
  102.     <td width="100" height="19"><font size="1">CDATA</font></td>
  103.     <td width="308" height="19"><font size="1"><![CDATA[....]]></font></td>
  104.     <td width="10" height="17"><font size="1">4</font></td>
  105.   </tr>
  106.   <tr>
  107.     <td width="100" height="19"><font size="1">EntityReference</font></td>
  108.     <td width="308" height="19"><font size="1">&foo;</font></td>
  109.     <td width="10" height="17"><font size="1">5</font></td>
  110.   </tr>
  111.   <tr>
  112.     <td width="100" height="19"><font size="1">Entity</font></td>
  113.     <td width="1000" height="19"><font size="1"><!ENTITY ...></font></td>
  114.     <td width="10" height="17"><font size="1">6</font></td>
  115.   </tr>
  116.   <tr>
  117.     <td width="100" height="19"><font size="1">ProcessingInstruction</font></td>
  118.     <td width="1000" height="19"><font size="1"><?pi test?></font></td>
  119.     <td width="10" height="17"><font size="1">7</font></td>
  120.   </tr>
  121.   <tr>
  122.     <td height="17"><font size="1">Comment</font></td>
  123.     <td height="17"><font size="1"><!-- comment --></font></td>
  124.     <td height="17"><font size="1">8</font></td>
  125.   </tr>
  126.   <tr>
  127.     <td width="100" height="19"><font size="1">Document</font></td>
  128.     <td width="308" height="19"><font size="1"></font></td>
  129.     <td width="10" height="17"><font size="1">9</font></td>
  130.   </tr>
  131.   <tr>
  132.     <td width="100" height="19"><font size="1">DocumentType</font></td>
  133.     <td width="308" height="19"><font size="1"><!DOCTYPE ...></font></td>
  134.     <td width="10" height="17"><font size="1">10</font></td>
  135.   </tr>
  136.   <tr>
  137.     <td width="100" height="19"><font size="1">DocumentFragment</font></td>
  138.     <td width="308" height="19"><font size="1"></font></td>
  139.     <td width="10" height="17"><font size="1">11</font></td>
  140.   </tr>
  141.   <tr>
  142.     <td width="100" height="19"><font size="1">Notation</font></td>
  143.     <td width="308" height="19"><font size="1"><!NOTATION ...></font></td>
  144.     <td width="10" height="17"><font size="1">12</font></td>
  145.   </tr>
  146.   <tr>
  147.     <td width="100" height="19"><font size="1">Whitespace</font></td>
  148.     <td width="308" height="19"><font size="1">Whitespace between markup.</font></td>
  149.     <td width="10" height="17"><font size="1">13</font></td>
  150.   </tr>
  151.   <tr>
  152.     <td width="100" height="19"><font size="1">SignificantWhitespace</font></td>
  153.     <td width="1000" height="19"><font size="1">Whitespace between markup in a mixed content model.</font></td>
  154.     <td width="10" height="17"><font size="1">14</font></td>
  155.   </tr>
  156.   <tr>
  157.     <td width="100" height="19"><font size="1">EndTag</font></td>
  158.     <td width="1000" height="19"><font size="1"></foo></font></td>
  159.     <td width="10" height="17"><font size="1">15</font></td>
  160.   </tr>
  161.   <tr>
  162.     <td width="100" height="19"><font size="1">EndEntity</font></td>
  163.     <td width="1000" height="19"><font size="1">Returned when the reader has gotten to the end of the entity replacement as a result of a call to ExpandEntity().</font></td>
  164.     <td width="10" height="17"><font size="1">16</font></td>
  165.   </tr>
  166.   <tr>
  167.     <td width="100" height="19"><font size="1">CharacterEntity</font></td>
  168.     <td width="1000" height="19"><font size="1">Returned when the reader has been told to report character entities (e.g. &#65;). See the 
  169.     EntityHandling property.</font></td>
  170.     <td width="10" height="17"><font size="1">17</font></td>
  171.   </tr>
  172.   </TBODY></TABLE></DIV>
  173. <br clear="left"><br>
  174.  
  175. <div style="width:660">
  176. The <b>Depth</b> property reports the depth of the current node and can be useful for formatting. Nodes at the root level are at depth 0. Combining this with
  177. the Name and Value properties we can create a sample which processes an XML file and formats the output depending on the node type and the depth,
  178. gathering statistics as it reads. The Format method, shown below, implements some basic formatting code to the console. The full code is at 
  179. <a href="/quickstart/util/srcview.aspx?path=/quickstart/howto/samples/Xml/ReadXmlFile/ReadXmlFile.src">View Source</a>.</div>
  180.  
  181. <div class="code"><xmp>
  182. private static void Format(XmlReader reader, String NodeType)
  183. {
  184.     // Format the output
  185.     Console.Write(reader.Depth + " ");
  186.     Console.Write(reader.AttributeCount + " ");
  187.     for (int i=0; i < reader.Depth; i++)
  188.     { 
  189.         Console.Write('\t');
  190.     }
  191.  
  192.     Console.Write(reader.Prefix + NodeType + "<" + reader.Name + ">" + reader.Value);
  193.  
  194.     // Display the attributes values for the current node
  195.     if (reader.HasAttributes)
  196.     {
  197.         Console.Write(" Attributes:");
  198.  
  199.         for (int j=0; j < reader.AttributeCount; j++)
  200.         {
  201.             Console.Write(" [{0}] " + reader[j], j);
  202.         }
  203.     }
  204.     Console.WriteLine();
  205. }
  206. </xmp></div>
  207. <div style="width:660">
  208. The <b>Prefix</b> property returns the namespace prefix associated with the node. Element node types can have a list of attribute nodes associated with them.
  209. Here we test whether the node has any attributes with the <b>HasAttributes</b> property and then use the node index operators to retrieve each attribute value.
  210. This is analogous to a collection of attributes for the node.
  211. The <b>AttributeCount</b> property returns the number of attributes for the current node. This approach is used if all you are interested in are the attribute
  212. values and are not concerned with other properties of the attribute nodes (e.g. The name of the attribute).
  213. In the <A target=content href="ReadXmlStream.aspx">How Do I...Read XML from a stream?</a> topic we show an alternative approach to accessing the attributes
  214. by moving to each attribute node in order to read both its name and value.</div>
  215.  
  216. <br clear="left"><div style="width:660">
  217. The output from running this sample with the 
  218. <a target="_blank" href="/quickstart/util/srcctrlwin.aspx?path=/quickstart/howto/samples/Xml/ReadXmlFile/&file=books.xml">books.xml</a> file
  219. is shown below. The first column is the Depth property and the second column is the AttributeCount property.</div>
  220.  
  221. <div class="code"><xmp>
  222. 0 0 ProcessingInstruction<xml>version='1.0'
  223. 0 0 Comment<> This file represents a fragment of a book store inventory database
  224. 0 0 Element<bookstore>
  225. 1 3     Element<book> Attributes: [0] autobiography [1] 1981 [2] 1-861003-11-0
  226. 2 0             Element<title>
  227. 3 0                     Text<>The Autobiography of Benjamin Franklin
  228. 2 0             Element<author>
  229. 3 0                     Element<first-name>
  230. 4 0                             Text<>Benjamin
  231. 3 0                     Element<last-name>
  232. 4 0                             Text<>Franklin
  233. 2 0             Element<price>
  234. 3 0                     Text<>8.99
  235. 1 3     Element<book> Attributes: [0] novel [1] 1967 [2] 0-201-63361-2
  236. 2 0             Element<title>
  237. 3 0                     Text<>The Confidence Man
  238. 2 0             Element<author>
  239. 3 0                     Element<first-name>
  240. 4 0                             Text<>Herman
  241. 3 0                     Element<last-name>
  242. 4 0                             Text<>Melville
  243. 2 0             Element<price>
  244. 3 0                     Text<>11.99
  245. 1 3     Element<book> Attributes: [0] philosophy [1] 1991 [2] 1-861001-57-6
  246. 2 0             Element<title>
  247. 3 0                     Text<>The Gorgias
  248. 2 0             Element<author>
  249. 3 0                     Element<name>
  250. 4 0                             Text<>Plato
  251. 2 0             Element<price>
  252. 3 0                     Text<>9.99
  253.  
  254. Statistics for books.xml file
  255.  
  256. ProcessingInstruction: 1
  257. DocumentType: 0
  258. Comment: 1
  259. Element: 18
  260. Attribute: 9
  261. Text: 11
  262. Whitespace: 27
  263. </xmp></div>
  264.  
  265. <H4>Summary</H4>
  266. <OL>
  267. <LI>The XmlTextReader provides fast, non-cached, forward only read access to XML data.
  268. <LI>The XmlTextReader implements the <A href="http://www.w3.org/TR/1998/REC-xml-19980210">W3C Extensible Markup 
  269. Language (XML) 1.0</A></A> specification and the <A><A href="http://www.w3.org/TR/REC-xml-names/">Namespaces in XML</A></A> specification. 
  270. <LI>The XmlTextReader provides constructors to read XML from a file, a stream or a TextReader.
  271. <LI>The Read method moves the reader sequentially through the records (or nodes).
  272. <LI>For element nodes, the value of an attribute can be obtained by using the index operators.
  273. <LI>Attributes are represented as a node list off the current node and can be discovered through the HasAttributes property.
  274. <LI>The Depth property reports the depth of the current node and can be useful for formatting. Nodes at the root level are at depth 0.
  275. <LI>The Name and Value properties provide details about the current node.
  276. </LI></OL>
  277.  
  278. <!-- #include virtual="/quickstart/howto/include/footer.inc" -->
  279.  
  280.  
  281.  
  282.  
  283.  
  284.  
  285.  
  286.  
  287.  
  288.  
  289.  
  290.  
  291.  
  292.  
  293.  
  294.  
  295.  
  296.  
  297.  
  298.  
  299.  
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.  
  335.  
  336.  
  337.  
  338.  
  339.  
  340.  
  341.  
  342.  
  343.  
  344.  
  345.  
  346.  
  347.  
  348.  
  349.  
  350.  
  351.  
  352.  
  353.  
  354.  
  355.  
  356.  
  357.  
  358.  
  359.  
  360.  
  361.  
  362.  
  363.  
  364.  
  365.  
  366.  
  367.  
  368.  
  369.  
  370.  
  371.  
  372.  
  373.  
  374.  
  375.  
  376.  
  377.  
  378.