home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 40 / IOPROG_40.ISO / SOFT / NETFrameworkSDK.exe / comsdk.cab / samples.exe / QuickStart / howto / doc / XML / StreamAnXmlDocument.aspx < prev    next >
Encoding:
Text File  |  2000-06-10  |  4.2 KB  |  94 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 an XmlDataDocument?</h4>
  6.  
  7. <div class="indent" style="width:660">
  8. This sample illustrates how to read XML from an XmlDataDocument by creating an XmlReader class
  9. that reads the XML data stored in the XmlDataDocument.
  10. This sample follows on from the <a href="LoadXmlDocument.aspx">How do I...Load XML data into an XmlDataDocument?</a> topic.</div>
  11.  
  12. <h4>Reading XML from an XmlDataDocument</h4> 
  13. <Acme:SourceRef
  14. ViewSource="/quickstart/howto/samples/Xml/StreamXmlDocument/StreamXmlDocument.src"
  15. RunSample="/quickstart/howto/samples/Xml/StreamXmlDocument/StreamXmlDocument.aspx" 
  16. Icon = "/quickstart/images/genicon.gif"
  17. Caption="StreamXmlDataDocument.aspx"
  18. runat="server" />
  19.  
  20. <br clear="left"><br>
  21. <div class="indent" style="width:660">
  22. As mentioned in the previous topic, the XmlDataDocument class is an in-memory cache for XML data. The DataDocumentReader provides an implementation of an XmlReader
  23. and consumes an XmlDataDocument to provide a mechanism for reading the data directly rather than using the W3C DOM methods. However the DataDocumentReader is not yet
  24. implemented so an alternative approach is demonstrated here to obtain an XmlReader. In this sample we are going to load the XmlDataDocument with XML data using
  25. the sample file <a target="_blank" href="/quickstart/util/srcctrlwin.aspx?path=/quickstart/howto/samples/Xml/StreamXmlDocument/&file=books.xml">books.xml</a>
  26. and display the loaded XML with an XmlReader. The XmlReader allows you to perform a sequential read on the XML data.
  27. See <a href="ReadXmlFile.aspx">How do I...Read XML from a file?</a> topic. The sample code below illustrates this.</div>
  28.  
  29. <div class="code">
  30. <xmp>
  31. private const String m_Document = "books.xml";
  32. private const String m_Stylesheet = "identity.xsl";
  33.  
  34. XmlReader docreader = null;
  35.  
  36. try
  37. {
  38.     // Load the XML from file
  39.     Console.WriteLine ("Loading file {0} ...", m_Document);
  40.  
  41.     XmlDataDocument xmldocument = new XmlDataDocument();
  42.     xmldocument.Load (m_Document);
  43.  
  44.     Console.WriteLine ("XmlDataDocument loaded with XML data successfully ...\r\n");
  45.  
  46.     // The use of an XslTransform is a work-around for getting an XmlReader from XmlDataDocument
  47.     // until DataDocumentReader is implemented.
  48.     // DataDocumentReader docreader = DataDocumentReader(xmldocument);
  49.  
  50.     Console.WriteLine ("Reading XML ...\r\n");
  51.  
  52.     XslTransform   xsltransform = new XslTransform();
  53.     xsltransform.Load(m_Stylesheet);
  54.     docreader = xsltransform.Transform(new DataDocumentNavigator(xmldocument), null);
  55.     FormatXml (docreader);
  56. }
  57.  
  58. catch (Exception e)
  59. {
  60.     Console.WriteLine ("Exception: {0}", e.ToString());
  61. }
  62.  
  63. finally
  64. {
  65.     // Close the reader
  66.     if (docreader != null)
  67.         docreader.Close();
  68. }
  69. </xmp>
  70. </div>
  71.  
  72. <div class="indent" style="width:660">
  73. Here we create an XslTransform object and load it with a style sheet,
  74. <a target="_blank" href="/quickstart/util/srcctrlwin.aspx?path=/quickstart/howto/samples/Xml/StreamXmlDocument/&file=identity.xsl">identity.xsl</a>,
  75. which replicates all the XML from the root node. Then by creating a DataDocumentNavigator onto the XmlDataDocument we can perform a transform on all the nodes
  76. and be returned a reader onto the transformed data. This reader can now be used to read the XML data in the XmlDataDocument.
  77. The DataDocumentNavigator class is covered in <A target=content href="NavigateXmlDocument.aspx">How Do I...Navigate XML with the XmlNavigator?</A> and 
  78. the XslTransform class is covered in <A target=content href="TransformXml.aspx">How Do I...Apply an XSL transformation to XML?</A></div>
  79.  
  80. <H4>Summary</H4>
  81. <OL>
  82. <LI>An XmlReader can be obtained for the XML data in the XmlDataDocument via an XslTransform.
  83. <LI>The DataDocumentReader class will provide an implementation of the XmlReader class to perform sequential reads on the XML data in an XmlDataDocument.
  84. <LI>The DataDocumentReader provides and alternative, faster mechanism for reading the XML from the in-memory cache, rather than using the DOM classes and methods.
  85. </LI></OL>
  86.  
  87. <!-- #include virtual="/quickstart/howto/include/footer.inc" -->
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.