home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 40 / IOPROG_40.ISO / SOFT / NETFrameworkSDK.exe / comsdk.cab / samples.exe / QuickStart / howto / doc / XML / LoadDataSetXMLData.aspx < prev    next >
Encoding:
Text File  |  2000-06-10  |  5.1 KB  |  149 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...Load a DataSet with XML?</h4>
  6.  
  7. <div class="indent" style="width:660">
  8. This sample illustrates how to load a DataSet with XML Data. The sample builds on the previous topic
  9. <a href="/quickstart/howto/doc/Xml/DataSetMapXSDSchema.aspx">How Do I...Create DataSet mappings from an XSD schema?</a> by first loading XML data into
  10. an XmlDataDocument and then accessing this data from the DataSet. The DataSet has previously loaded a schema in order to create the internal mappings.
  11. This sample in effect shows the transition between XML data and the creation of relational objects to access that XML data.</div>
  12.  
  13. <h4>Loading a DataSet with XML</h4> 
  14. <Acme:SourceRef
  15. ViewSource="/quickstart/howto/samples/Xml/LoadDataSetXMLData/LoadDataSetXMLData.src"
  16. RunSample="/quickstart/howto/samples/Xml/LoadDataSetXMLData/LoadDataSetXMLData.aspx" 
  17. Icon = "/quickstart/images/genicon.gif"
  18. Caption="LoadDataSetXMLData.aspx"
  19. runat="server" />
  20.  
  21. <br clear="left"><br>
  22. <div class="indent" style="width:660">
  23. First the XSD schema <a target="_blank" href="/quickstart/util/srcctrlwin.aspx?path=/quickstart/howto/samples/Xml/LoadDataSetXMLData/&file=books.xsd">books.xsd</a>
  24. is loaded into the <b>DataSet</b> property on the XmlDataDocument and the XML file
  25. <a target="_blank" href="/quickstart/util/srcctrlwin.aspx?path=/quickstart/howto/samples/Xml/LoadDataSetXMLData/&file=books.xml">books.xml</a> is loaded with 
  26. the XmlDataDocument <b>Load</b> method. This is shown in the code below.</div>
  27.  
  28. <div class="code"><xmp>
  29. private const String m_Document = "books.xml";
  30. private const String m_Schema = "books.xsd";
  31.  
  32. XmlDataDocument m_datadoc = new XmlDataDocument();
  33. ParseSchema(m_Schema);
  34. DisplayTableStructure();
  35.  
  36. m_datadoc.Load(m_Document);
  37.  
  38. DisplayTables(m_datadoc.DataSet);
  39. </xmp></div>
  40.  
  41. <div class="indent" style="width:660">
  42. As in the previous topic, by simply iterating over the collections of Tables, Columns and Rows and formatting the output we can display both the internal
  43. table structure with the DisplayTableStructure() method (built with the
  44. <a target="_blank" href="/quickstart/util/srcctrlwin.aspx?path=/quickstart/howto/samples/Xml/LoadDataSetXMLData/&file=books.xsd">books.xsd</a> schema file)
  45. and the content of the XML file with the DisplayTables() method. This time we use the foreach keyword instead of a for loop to illustrate an alternative
  46. mechanism for iterating over the collections. See <a href="/quickstart/howto/doc/Xml/DataSetMapXSDSchema.aspx">How Do I...Create DataSet mappings from an XSD schema?</a>
  47. </div>
  48.  
  49. <div class="code"><xmp>
  50. // Displays the contents of the DataSet tables
  51. private void DisplayTables(DataSet dataset)
  52. {
  53.     // Navigate Dataset
  54.     Console.WriteLine("Content of Tables ...\r\n");
  55.  
  56.     foreach(DataTable table in dataset.Tables)
  57.     {
  58.         Console.WriteLine("TableName = " + table.TableName);
  59.         Console.WriteLine ("{0}", "---------");
  60.         Console.WriteLine("Columns ...\r\n");
  61.  
  62.         foreach(DataColumn column in table.Columns)
  63.         {
  64.             Console.Write("{0,-22}",column.ColumnName);
  65.         }
  66.         Console.WriteLine("");
  67.         Console.WriteLine("\r\nNumber of rows = {0}", table.Rows.All.Length);
  68.         Console.WriteLine("Rows ...\r\n");
  69.  
  70.         foreach(DataRow row in table.Rows)
  71.         {
  72.             foreach(Object value in row.ItemArray)
  73.             {
  74.                 Console.Write("{0,-22}",value.ToString());
  75.             }
  76.             Console.WriteLine();
  77.         }
  78.         Console.WriteLine();
  79.     }
  80. }
  81. </xmp></div>
  82.  
  83. <div class="indent" style="width:660">
  84. The output from DisplayTables() for the books.xml is illustrated below showing the table and column names and the row contents.
  85. </div>
  86.  
  87. <div class="code"><xmp>
  88. Content of Tables ...
  89.  
  90. TableName = book
  91. ---------
  92. Columns ...
  93.  
  94. genre                 publicationdate       ISBN                  title                 book_Id               price
  95.  
  96. Number of rows = 3
  97. Rows ...
  98.  
  99. autobiography         1981                  1-861003-11-0         The Autobiography of Benjamin Franklin 0    8.99
  100. novel                 1967                  0-201-63361-2         The Confidence Man    1                     11.99
  101. philosophy            1991                  1-861001-57-6         The Gorgias           2                     9.99
  102.  
  103. TableName = author
  104. ---------
  105. Columns ...
  106.  
  107. first-name            last-name             book_Id
  108.  
  109. Number of rows = 3
  110. Rows ...
  111.  
  112. Benjamin              Franklin              0
  113. Herman                Melville              1
  114. Sidas                 Plato                 2
  115. </xmp></div>
  116.  
  117. <H4>Summary</H4>
  118. <OL>
  119. <LI>XML data which has been loaded into an XmlDataDocument can be accessed via the relational methods on the DataSet property.
  120. <LI>XML data can also be read when relational data has been entered via the XmlDataDocument, DataSet property.
  121. </LI></OL>
  122.  
  123. <!-- #include virtual="/quickstart/howto/include/footer.inc" -->
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
  148.  
  149.