Listing 157 Generated XSD Schema for the Class Records Students Table

<xsd:schema id="Ch15 dsClassRecords Students" targetNamespace=

" dsClassRecords Students.xsd" xmlns = " dsClassRecords Students.xsd" xmlns:xsd="" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" attributeFormDefault="qualified" elementFormDefault="qualified"> <xsd:element name="Ch15 dsClassRecords Students" msdata:IsDataSet="true"> <xsd:complexType>

<xsd:choice maxOccurs="unbounded"> <xsd:element name="Students"> <xsd:complexType> <xsd:sequence>

<xsd:element name="StudentID" msdata:ReadOnly="true"

msdata:AutoIncrement="true" type="xsd:int" /> <xsd:element name="Grade" type="xsd:unsignedByte" /> <xsd:element name="LastName" type="xsd:string" /> <xsd:element name="FirstName" type="xsd:string" /> </xsd:sequence>

</xsd:complexType> </xsd:element> </xsd:choice> </xsd:complexType>

<xsd:unique name="Constraint1" msdata:PrimaryKey="true"> <xsd:selector xpath=".//Students" /> <xsd:field xpath="StudentID" /> </xsd:unique> </xsd:element> </xsd:schema>

The schema shows you how the XML representation of the data will look. For example, the root element will be <students>; each field becomes an element in a sequence that follows the order of the fields in the SQL query for the SqlDataAdapter. Notice that the studentiD element has an attribute with the name and value msdata:Autoincrement="True", meaning that the schema understands that the StudentiD field is marked as an Identity field in SQL Server. Also, look at the constraint definition toward the end of the schema. The schema also captures the fact that the StudentiD is a primary key as well as the XPath query to select the <students> and the name of the primary key field.

Warning XML is case sensitive, so be careful with your SQL and XPath query names.

The generated class is interesting as well, and I urge you to look at it, although due to its size, I won't show it here. You should not alter the generated code manually because the system will overwrite the code if you later regenerate or alter the DataSet.

You can now use the Fill method to fill ch15_dsClassRecords_Students DataSet and print the result to the browser so you can see the XML. For example, the Form_Load method in the ch15-4.aspx Web Form displays the DataSet's XML document contents using very little hand-generated code. One of the reasons it uses so little code is that the SqlDataAdapter.Fill method opens and closes the associated Connection object automatically, so you don't have to do it yourself. However, as you've seen, if you write the code, you have control over what can be seen when page tracing is enabled, whereas when you rely on the designers, you don't.

private void Page Load(object sender, System.EventArgs e) { DataSet ds = new ch15 dsClassRecords Students(); sqlDataAdapter1.Fill(ds);




You can see from the result that the DataSet does indeed contain the data from the Students table in XML form, but the results aren't very satisfying. Fortunately, the GetXml method's return value already contains line breaks and indentation. If you place the results of the DataSet.GetXml call between <pre></pre> tags, you'll get a better format. Here's another version with better output (see Figure 15.6).

private void Page Load(object sender, System.EventArgs e) { DataSet ds = new ch15 dsClassRecords Students(); sqlDataAdapter1.Fill(ds);

Response.Write("<pre>" + Server.HtmlEncode(ds.GetXml()) + "</pre>");

sqlDataAdapter1.Dispose(); Response.End();

Figure 15.6: Formatted contents of the Ch15_dsClassRecords_Students1 dataset

Was this article helpful?

0 0

Post a comment