home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 40 / IOPROG_40.ISO / SOFT / NETFrameworkSDK.exe / comsdk.cab / samples.exe / QuickStart / howto / doc / XML / DataSetMapXSDSchema.aspx < prev    next >
Encoding:
Text File  |  2000-06-10  |  4.5 KB  |  100 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...Create DataSet mappings from an XSD schema?</h4>
  6.  
  7. <div class="indent" style="width:660">
  8. This sample illustrates how to create DataSet mappings from a supplied XSD schema. Generally we can refer to schemas as metadata, or data about data, but XSD schemas
  9. also cover the relationships between types of data. From a schema we can create a relational structure of tables and columns to store data
  10. that fits the supplied schema. This is the DataSet relational mapping of the schema.</div>
  11.  
  12. <h4>Creating DataSet mappings from an XSD schema</h4> 
  13. <Acme:SourceRef
  14. ViewSource="/quickstart/howto/samples/Xml/DataSetMapXSDSchema/DataSetMapXSDSchema.src"
  15. RunSample="/quickstart/howto/samples/Xml/DataSetMapXSDSchema/DataSetMapXSDSchema.aspx" 
  16. Icon = "/quickstart/images/genicon.gif"
  17. Caption="DataSetMapXSDSchema.aspx"
  18. runat="server" />
  19.  
  20. <br clear="left"><br>
  21. <div class="indent" style="width:660">
  22. In this sample the <a target="_blank" href="/quickstart/util/srcctrlwin.aspx?path=/quickstart/howto/samples/Xml/DataSetMapXSDSchema/&file=books.xsd">books.xsd</a>
  23. schema file is read and loaded into the DataSet property on the XmlDataDocument. The <b>ReadXmlSchema</b> method takes the schema loaded into
  24. a StreamReader
  25. and generates the relational mappings. The code sample below illustrates this.
  26. </div>
  27.  
  28. <div class="code">
  29. <xmp>
  30. StreamReader streamreader_schema = new StreamReader(schema);
  31. Console.WriteLine("Reading Schema file ...");
  32. m_datadoc.DataSet.ReadXmlSchema(streamreader_schema);
  33. streamreader_schema.Close();
  34. </xmp></div>
  35.  
  36. <div class="indent" style="width:660">
  37. If there is already a relational view defined in the XmlDataDocument then an exception is thrown. So how do you know what the internal tables
  38. that have been
  39. generated from the schema look like? Well, the DataSet has a <b>Tables</b> property
  40. that is a collection of the internal tables. Each table has a <b>Columns</b> collection and each column has a <b>ColumnName</b> and a <b>DataType</b>.
  41. Simply iterating over these collections and formatting the output we can display the internal table structure built from the supplied schema.
  42. The code sample below illustrates this.
  43. </div>
  44.  
  45. <div class="code">
  46. <xmp>
  47. // Displays the DataSet tables structure
  48. private void DisplayTableStructure()
  49. {
  50.     Console.WriteLine("\r\nTable structure \r\n");
  51.     Console.WriteLine("Tables count=" + m_datadoc.DataSet.Tables.Count.ToString());
  52.     for (int i = 0; i < m_datadoc.DataSet.Tables.Count; i++)
  53.     {
  54.         Console.WriteLine("\tTableName='" + m_datadoc.DataSet.Tables[i].TableName + "'.");
  55.         Console.WriteLine("\tColumns count=" + m_datadoc.DataSet.Tables[i].Columns.Count.ToString());
  56.  
  57.         for (int j = 0; j < m_datadoc.DataSet.Tables[i].Columns.Count; j++)
  58.         {
  59.             Console.WriteLine("\t\tColumnName='" + m_datadoc.DataSet.Tables[i].Columns[j].ColumnName + "',
  60.              type = " + m_datadoc.DataSet.Tables[i].Columns[j].DataType.ToString());
  61.         }
  62.     }
  63. }
  64. </xmp></div>
  65.  
  66. <div class="indent" style="width:660">
  67. The output from DisplayTableStructure() for the books.xsd schema is illustrated below showing the table and column names and the column types.
  68. </div>
  69.  
  70. <div class="code">
  71. <xmp>
  72. Table structure
  73.  
  74. Tables count=2
  75.         TableName='book'.
  76.         Columns count=6
  77.                 ColumnName='genre', type = System.String
  78.                 ColumnName='publicationdate', type = System.String
  79.                 ColumnName='ISBN', type = System.String
  80.                 ColumnName='title', type = System.String
  81.                 ColumnName='book_Id', type = Int32
  82.                 ColumnName='price', type = System.String
  83.         TableName='author'.
  84.         Columns count=3
  85.                 ColumnName='first-name', type = System.String
  86.                 ColumnName='last-name', type = System.String
  87.                 ColumnName='book_Id', type = Int32
  88. </xmp></div>
  89.  
  90.  
  91. <H4>Summary</H4>
  92. <OL>
  93. <LI>Generally we can refer to schemas as metadata, or data about data, but XSD schemas also cover the relationships between types of data.
  94. <LI>From an XSD schema we can create a relational structure of tables and columns to store data
  95.   that fits the supplied schema. This is the DataSet mapping.
  96. <LI>The DataSet method ReadXmlSchema generates the internal mappings from the supplied schema.
  97. </LI></OL>
  98.  
  99.     <!-- #include virtual="/quickstart/howto/include/footer.inc" -->
  100.