home *** CD-ROM | disk | FTP | other *** search
Wrap
<%@ Register TagPrefix="Acme" TagName="SourceRef" Src="/quickstart/util/SrcRef.aspx"%> <!-- #include virtual="/quickstart/howto/doc/xml/header.inc" --> <h4>How Do I...Create DataSet mappings from an XSD schema?</h4> <div class="indent" style="width:660"> 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 also cover the relationships between types of data. From a schema we can create a relational structure of tables and columns to store data that fits the supplied schema. This is the DataSet relational mapping of the schema.</div> <h4>Creating DataSet mappings from an XSD schema</h4> <Acme:SourceRef ViewSource="/quickstart/howto/samples/Xml/DataSetMapXSDSchema/DataSetMapXSDSchema.src" RunSample="/quickstart/howto/samples/Xml/DataSetMapXSDSchema/DataSetMapXSDSchema.aspx" Icon = "/quickstart/images/genicon.gif" Caption="DataSetMapXSDSchema.aspx" runat="server" /> <br clear="left"><br> <div class="indent" style="width:660"> In this sample the <a target="_blank" href="/quickstart/util/srcctrlwin.aspx?path=/quickstart/howto/samples/Xml/DataSetMapXSDSchema/&file=books.xsd">books.xsd</a> schema file is read and loaded into the DataSet property on the XmlDataDocument. The <b>ReadXmlSchema</b> method takes the schema loaded into a StreamReader and generates the relational mappings. The code sample below illustrates this. </div> <div class="code"> <xmp> StreamReader streamreader_schema = new StreamReader(schema); Console.WriteLine("Reading Schema file ..."); m_datadoc.DataSet.ReadXmlSchema(streamreader_schema); streamreader_schema.Close(); </xmp></div> <div class="indent" style="width:660"> 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 that have been generated from the schema look like? Well, the DataSet has a <b>Tables</b> property 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>. Simply iterating over these collections and formatting the output we can display the internal table structure built from the supplied schema. The code sample below illustrates this. </div> <div class="code"> <xmp> // Displays the DataSet tables structure private void DisplayTableStructure() { Console.WriteLine("\r\nTable structure \r\n"); Console.WriteLine("Tables count=" + m_datadoc.DataSet.Tables.Count.ToString()); for (int i = 0; i < m_datadoc.DataSet.Tables.Count; i++) { Console.WriteLine("\tTableName='" + m_datadoc.DataSet.Tables[i].TableName + "'."); Console.WriteLine("\tColumns count=" + m_datadoc.DataSet.Tables[i].Columns.Count.ToString()); for (int j = 0; j < m_datadoc.DataSet.Tables[i].Columns.Count; j++) { Console.WriteLine("\t\tColumnName='" + m_datadoc.DataSet.Tables[i].Columns[j].ColumnName + "', type = " + m_datadoc.DataSet.Tables[i].Columns[j].DataType.ToString()); } } } </xmp></div> <div class="indent" style="width:660"> The output from DisplayTableStructure() for the books.xsd schema is illustrated below showing the table and column names and the column types. </div> <div class="code"> <xmp> Table structure Tables count=2 TableName='book'. Columns count=6 ColumnName='genre', type = System.String ColumnName='publicationdate', type = System.String ColumnName='ISBN', type = System.String ColumnName='title', type = System.String ColumnName='book_Id', type = Int32 ColumnName='price', type = System.String TableName='author'. Columns count=3 ColumnName='first-name', type = System.String ColumnName='last-name', type = System.String ColumnName='book_Id', type = Int32 </xmp></div> <H4>Summary</H4> <OL> <LI>Generally we can refer to schemas as metadata, or data about data, but XSD schemas also cover the relationships between types of data. <LI>From an XSD schema we can create a relational structure of tables and columns to store data that fits the supplied schema. This is the DataSet mapping. <LI>The DataSet method ReadXmlSchema generates the internal mappings from the supplied schema. </LI></OL> <!-- #include virtual="/quickstart/howto/include/footer.inc" -->