15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

XMl schemas: Generating Code with XsD ❘ 845<br />

Thankfully, there is a tool within the .<strong>NET</strong> Framework that produces the code for these classes with the<br />

help of the input XSD file. Because its sole job is to perform various functions on XSD files, the tool itself is<br />

called XSD.EXE.<br />

Assuming that you saved the preceding file as Product.xsd, you would convert the file into code by issuing<br />

the following comm<strong>and</strong> in a comm<strong>and</strong> prompt:<br />

xsd Product.xsd /d<br />

This creates the file Product.cs.<br />

Various switches can be used with XSD to alter the output generated. Some of the more commonly used<br />

switches are shown in the following table.<br />

sWiTCh<br />

/dataset (/d)<br />

/language:<br />

/namespace:<br />

desCriPTion<br />

Enables you to generate classes derived from DataSet, DataTable, <strong>and</strong><br />

DataRow.<br />

Permits you to choose which language the output file will be written in. <strong>C#</strong> is<br />

the default, but you can choose VB for a Visual Basic .<strong>NET</strong> file.<br />

Enables you to define the namespace that the generated code should reside<br />

within. The default is no namespace.<br />

The following is an abridged version of the output from XSD for the Products schema. The output has<br />

been altered slightly to fit into a format appropriate for this book. To see the complete output, run XSD.<br />

EXE on the Products schema (or one of your own making) <strong>and</strong> look at the .cs file generated. The example<br />

includes the entire source code plus the Product.xsd file:<br />

//--------------------------------------<br />

// <br />

// This code was generated by a tool.<br />

// Runtime Version:4.0.21006.1<br />

//<br />

// Changes to this file may cause incorrect behavior <strong>and</strong> will be lost if<br />

// the code is regenerated.<br />

// <br />

//--------------------------------------<br />

//<br />

// This source code was auto-generated by xsd, Version=4.0.21006.1<br />

//<br />

/// <br />

///Represents a strongly typed in-memory cache of data.<br />

///<br />

[global::System.Serializable()]<br />

[global::System.ComponentModel.DesignerCategoryAttribute("code")]<br />

[global::System.ComponentModel.ToolboxItem(true)]<br />

[global::System.Xml.Serialization.XmlSchemaProviderAttribute("GetTypedDataSetSchema")]<br />

[global::System.Xml.Serialization.XmlRootAttribute("Products")]<br />

[global::System.ComponentModel.Design.HelpKeywordAttribute("vs.data.DataSet")]<br />

public partial class Products : global::System.Data.DataSet {<br />

private ProductDataTable tableProduct;<br />

private global::System.Data.SchemaSerializationMode _schemaSerializationMode =<br />

global::System.Data.SchemaSerializationMode.IncludeSchema;<br />

[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]<br />

[global::System.CodeDom.Compiler.GeneratedCodeAttribute(<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!