使用xsd生成代码
前一段时间看《C#高级编程》发现用数据表对应的Xml文件可以生成cs代码。如:写一个xsd的文件:
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema id="Products" targetNamespace="http://tempuri.org/XMLSchema1.xsd" elementFormDefault="qualified" xmlns="http://tempuri.org/XMLSchema1.xsd" xmlns:mstns="http://tempuri.org/XMLSchema1.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="Product">
<xs:complexType>
<xs:sequence>
<xs:element name="ProductID" msdata:ReadOnly="true" msdata:AutoIncrement="true" type="xs:int" />
<xs:element name="ProductName" type="xs:string" />
<xs:element name="SupplierID" type="xs:int" minOccurs="0" />
<xs:element name="CategoryID" type="xs:int" minOccurs="0" />
<xs:element name="QuantityPerUnit" type="xs:string" minOccurs="0" />
<xs:element name="UnitPrice" type="xs:decimal" minOccurs="0" />
<xs:element name="UnitsInStock" type="xs:short" minOccurs="0" />
<xs:element name="UnitsOnOrder" type="xs:short" minOccurs="0" />
<xs:element name="ReorderLevel" type="xs:short" minOccurs="0" />
<xs:element name="Discontinued" type="xs:boolean" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
保存为Product.xsd文件,在命令控制行中输入命令:
xsd /dataset /language:CS Product.xsd
会生成一个Product.cs文件
//------------------------------------------------------------------------------
// <autogenerated>
// This code was generated by a tool.
// Runtime Version: 1.1.4322.2300
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </autogenerated>
//------------------------------------------------------------------------------
//
// 此源代码由 xsd, Version=1.1.4322.2300 自动生成。
//
using System;
using System.Data;
using System.Xml;
using System.Runtime.Serialization;
[Serializable()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Diagnostics.DebuggerStepThrough()]
[System.ComponentModel.ToolboxItem(true)]
public class Products : DataSet {
private ProductDataTable tableProduct;
public Products() {
this.InitClass();
System.ComponentModel.CollectionChangeEventHandler schemaChangedHandler = new System.ComponentModel.CollectionChangeEventHandler(this.SchemaChanged);
this.Tables.CollectionChanged += schemaChangedHandler;
this.Relations.CollectionChanged += schemaChangedHandler;
}
protected Products(SerializationInfo info, StreamingContext context) {
string strSchema = ((string)(info.GetValue("XmlSchema", typeof(string))));
if ((strSchema != null)) {
DataSet ds = new DataSet();
ds.ReadXmlSchema(new XmlTextReader(new System.IO.StringReader(strSchema)));
if ((ds.Tables["Product"] != null)) {
this.Tables.Add(new ProductDataTable(ds.Tables["Product"]));
}
this.DataSetName = ds.DataSetName;
this.Prefix = ds.Prefix;
this.Namespace = ds.Namespace;
this.Locale = ds.Locale;
this.CaseSensitive = ds.CaseSensitive;
this.EnforceConstraints = ds.EnforceConstraints;
this.Merge(ds, false, System.Data.MissingSchemaAction.Add);
this.InitVars();
}
else {
this.InitClass();
}
this.GetSerializationData(info, context);
System.ComponentModel.CollectionChangeEventHandler schemaChangedHandler = new System.ComponentModel.CollectionChangeEventHandler(this.SchemaChanged);
this.Tables.CollectionChanged += schemaChangedHandler;
this.Relations.CollectionChanged += schemaChangedHandler;
}
[System.ComponentModel.Browsable(false)]
[System.ComponentModel.DesignerSerializationVisibilityAttribute(System.ComponentModel.DesignerSerializationVisibility.Content)]
public ProductDataTable Product {
get {
return this.tableProduct;
}
}
public override DataSet Clone() {
Products cln = ((Products)(base.Clone()));
cln.InitVars();
return cln;
}
protected override bool ShouldSerializeTables() {
return false;
}
protected override bool ShouldSerializeRelations() {
return false;
}
protected override void ReadXmlSerializable(XmlReader reader) {
this.Reset();
DataSet ds = new DataSet();
ds.ReadXml(reader);
if ((ds.Tables["Product"] != null)) {
this.Tables.Add(new ProductDataTable(ds.Tables["Product"]));
}
this.DataSetName = ds.DataSetName;
this.Prefix = ds.Prefix;
this.Namespace = ds.Namespace;
this.Locale = ds.Locale;
this.CaseSensitive = ds.CaseSensitive;
this.EnforceConstraints = ds.EnforceConstraints;
this.Merge(ds, false, System.Data.MissingSchemaAction.Add);
this.InitVars();
}
protected override System.Xml.Schema.XmlSchema GetSchemaSerializable() {
System.IO.MemoryStream stream = new System.IO.MemoryStream();
this.WriteXmlSchema(new XmlTextWriter(stream, null));
stream.Position = 0;
return System.Xml.Schema.XmlSchema.Read(new XmlTextReader(stream), null);
}
internal void InitVars() {
this.tableProduct = ((ProductDataTable)(this.Tables["Product"]));
if ((this.tableProduct != null)) {
this.tableProduct.InitVars();
}
}
private void InitClass() {
this.DataSetName = "Products";
this.Prefix = "";
this.Namespace = "http://tempuri.org/XMLSchema1.xsd";
this.Locale = new System.Globalization.CultureInfo("zh-CN");
this.CaseSensitive = false;
this.EnforceConstraints = true;
this.tableProduct = new ProductDataTable();
this.Tables.Add(this.tableProduct);
}
private bool ShouldSerializeProduct() {
return false;
}
private void SchemaChanged(object sender, System.ComponentModel.CollectionChangeEventArgs e) {
if ((e.Action == System.ComponentModel.CollectionChangeAction.Remove)) {
this.InitVars();
}
}
public delegate void ProductRowChangeEventHandler(object sender, ProductRowChangeEvent e);
[System.Diagnostics.DebuggerStepThrough()]
public class ProductDataTable : DataTable, System.Collections.IEnumerable {
private DataColumn columnProductID;
private DataColumn columnProductName;
private DataColumn columnSupplierID;
private DataColumn columnCategoryID;
private DataColumn columnQuantityPerUnit;
private DataColumn columnUnitPrice;
private DataColumn columnUnitsInStock;
private DataColumn columnUnitsOnOrder;
private DataColumn columnReorderLevel;
private DataColumn columnDiscontinued;
internal ProductDataTable() :
base("Product") {
this.InitClass();
}
internal ProductDataTable(DataTable table) :
base(table.TableName) {
if ((table.CaseSensitive != table.DataSet.CaseSensitive)) {
this.CaseSensitive = table.CaseSensitive;
}
if ((table.Locale.ToString() != table.DataSet.Locale.ToString())) {
this.Locale = table.Locale;
}
if ((table.Namespace != table.DataSet.Namespace)) {
this.Namespace = table.Namespace;
}
this.Prefix = table.Prefix;
this.MinimumCapacity = table.MinimumCapacity;
this.DisplayExpression = table.DisplayExpression;
}
[System.ComponentModel.Browsable(false)]
public int Count {
get {
return this.Rows.Count;
}
}
internal DataColumn ProductIDColumn {
get {
return this.columnProductID;
}
}
internal DataColumn ProductNameColumn {
get {
return this.columnProductName;
}
}
internal DataColumn SupplierIDColumn {
get {
return this.columnSupplierID;
}
}
internal DataColumn CategoryIDColumn {
get {
return this.columnCategoryID;
}
}
internal DataColumn QuantityPerUnitColumn {
get {
return this.columnQuantityPerUnit;
}
}
internal DataColumn UnitPriceColumn {
get {
return this.columnUnitPrice;
}
}
internal DataColumn UnitsInStockColumn {
get {
return this.columnUnitsInStock;
}
}
internal DataColumn UnitsOnOrderColumn {
get {
return this.columnUnitsOnOrder;
}
}
internal DataColumn ReorderLevelColumn {
get {
return this.columnReorderLevel;
}
}
internal DataColumn DiscontinuedColumn {
get {
return this.columnDiscontinued;
}
}
public ProductRow this[int index] {
get {
return ((ProductRow)(this.Rows[index]));
}
}
public event ProductRowChangeEventHandler ProductRowChanged;
public event ProductRowChangeEventHandler ProductRowChanging;
public event ProductRowChangeEventHandler ProductRowDeleted;
public event ProductRowChangeEventHandler ProductRowDeleting;
public void AddProductRow(ProductRow row) {
this.Rows.Add(row);
}
public ProductRow AddProductRow(string ProductName, int SupplierID, int CategoryID, string QuantityPerUnit, System.Decimal UnitPrice, short UnitsInStock, short UnitsOnOrder, short ReorderLevel, bool Discontinued) {
ProductRow rowProductRow = ((ProductRow)(this.NewRow()));
rowProductRow.ItemArray = new object[] {
null,
ProductName,
SupplierID,
CategoryID,
QuantityPerUnit,
UnitPrice,
UnitsInStock,
UnitsOnOrder,
ReorderLevel,
Discontinued};
this.Rows.Add(rowProductRow);
return rowProductRow;
}
public System.Collections.IEnumerator GetEnumerator() {
return this.Rows.GetEnumerator();
}
public override DataTable Clone() {
ProductDataTable cln = ((ProductDataTable)(base.Clone()));
cln.InitVars();
return cln;
}
protected override DataTable CreateInstance() {
return new ProductDataTable();
}
internal void InitVars() {
this.columnProductID = this.Columns["ProductID"];
this.columnProductName = this.Columns["ProductName"];
this.columnSupplierID = this.Columns["SupplierID"];
this.columnCategoryID = this.Columns["CategoryID"];
this.columnQuantityPerUnit = this.Columns["QuantityPerUnit"];
this.columnUnitPrice = this.Columns["UnitPrice"];
this.columnUnitsInStock = this.Columns["UnitsInStock"];
this.columnUnitsOnOrder = this.Columns["UnitsOnOrder"];
this.columnReorderLevel = this.Columns["ReorderLevel"];
this.columnDiscontinued = this.Columns["Discontinued"];
}
private void InitClass() {
this.columnProductID = new DataColumn("ProductID", typeof(int), null, System.Data.MappingType.Element);
this.Columns.Add(this.columnProductID);
this.columnProductName = new DataColumn("ProductName", typeof(string), null, System.Data.MappingType.Element);
this.Columns.Add(this.columnProductName);
this.columnSupplierID = new DataColumn("SupplierID", typeof(int), null, System.Data.MappingType.Element);
this.Columns.Add(this.columnSupplierID);
this.columnCategoryID = new DataColumn("CategoryID", typeof(int), null, System.Data.MappingType.Element);
this.Columns.Add(this.columnCategoryID);
this.columnQuantityPerUnit = new DataColumn("QuantityPerUnit", typeof(string), null, System.Data.MappingType.Element);
this.Columns.Add(this.columnQuantityPerUnit);
this.columnUnitPrice = new DataColumn("UnitPrice", typeof(System.Decimal), null, System.Data.MappingType.Element);
this.Columns.Add(this.columnUnitPrice);
this.columnUnitsInStock = new DataColumn("UnitsInStock", typeof(short), null, System.Data.MappingType.Element);
this.Columns.Add(this.columnUnitsInStock);
this.columnUnitsOnOrder = new DataColumn("UnitsOnOrder", typeof(short), null, System.Data.MappingType.Element);
this.Columns.Add(this.columnUnitsOnOrder);
this.columnReorderLevel = new DataColumn("ReorderLevel", typeof(short), null, System.Data.MappingType.Element);
this.Columns.Add(this.columnReorderLevel);
this.columnDiscontinued = new DataColumn("Discontinued", typeof(bool), null, System.Data.MappingType.Element);
this.Columns.Add(this.columnDiscontinued);
this.columnProductID.AutoIncrement = true;
this.columnProductID.AllowDBNull = false;
this.columnProductID.ReadOnly = true;
this.columnProductName.AllowDBNull = false;
this.columnDiscontinued.AllowDBNull = false;
}
public ProductRow NewProductRow() {
return ((ProductRow)(this.NewRow()));
}
protected override DataRow NewRowFromBuilder(DataRowBuilder builder) {
return new ProductRow(builder);
}
protected override System.Type GetRowType() {
return typeof(ProductRow);
}
protected override void OnRowChanged(DataRowChangeEventArgs e) {
base.OnRowChanged(e);
if ((this.ProductRowChanged != null)) {
this.ProductRowChanged(this, new ProductRowChangeEvent(((ProductRow)(e.Row)), e.Action));
}
}
protected override void OnRowChanging(DataRowChangeEventArgs e) {
base.OnRowChanging(e);
if ((this.ProductRowChanging != null)) {
this.ProductRowChanging(this, new ProductRowChangeEvent(((ProductRow)(e.Row)), e.Action));
}
}
protected override void OnRowDeleted(DataRowChangeEventArgs e) {
base.OnRowDeleted(e);
if ((this.ProductRowDeleted != null)) {
this.ProductRowDeleted(this, new ProductRowChangeEvent(((ProductRow)(e.Row)), e.Action));
}
}
protected override void OnRowDeleting(DataRowChangeEventArgs e) {
base.OnRowDeleting(e);
if ((this.ProductRowDeleting != null)) {
this.ProductRowDeleting(this, new ProductRowChangeEvent(((ProductRow)(e.Row)), e.Action));
}
}
public void RemoveProductRow(ProductRow row) {
this.Rows.Remove(row);
}
}
[System.Diagnostics.DebuggerStepThrough()]
public class ProductRow : DataRow {
private ProductDataTable tableProduct;
internal ProductRow(DataRowBuilder rb) :
base(rb) {
this.tableProduct = ((ProductDataTable)(this.Table));
}
public int ProductID {
get {
return ((int)(this[this.tableProduct.ProductIDColumn]));
}
set {
this[this.tableProduct.ProductIDColumn] = value;
}
}
public string ProductName {
get {
return ((string)(this[this.tableProduct.ProductNameColumn]));
}
set {
this[this.tableProduct.ProductNameColumn] = value;
}
}
public int SupplierID {
get {
try {
return ((int)(this[this.tableProduct.SupplierIDColumn]));
}
catch (InvalidCastException e) {
throw new StrongTypingException("无法获取值,因为它是 DBNull。", e);
}
}
set {
this[this.tableProduct.SupplierIDColumn] = value;
}
}
public int CategoryID {
get {
try {
return ((int)(this[this.tableProduct.CategoryIDColumn]));
}
catch (InvalidCastException e) {
throw new StrongTypingException("无法获取值,因为它是 DBNull。", e);
}
}
set {
this[this.tableProduct.CategoryIDColumn] = value;
}
}
public string QuantityPerUnit {
get {
try {
return ((string)(this[this.tableProduct.QuantityPerUnitColumn]));
}
catch (InvalidCastException e) {
throw new StrongTypingException("无法获取值,因为它是 DBNull。", e);
}
}
set {
this[this.tableProduct.QuantityPerUnitColumn] = value;
}
}
public System.Decimal UnitPrice {
get {
try {
return ((System.Decimal)(this[this.tableProduct.UnitPriceColumn]));
}
catch (InvalidCastException e) {
throw new StrongTypingException("无法获取值,因为它是 DBNull。", e);
}
}
set {
this[this.tableProduct.UnitPriceColumn] = value;
}
}
public short UnitsInStock {
get {
try {
return ((short)(this[this.tableProduct.UnitsInStockColumn]));
}
catch (InvalidCastException e) {
throw new StrongTypingException("无法获取值,因为它是 DBNull。", e);
}
}
set {
this[this.tableProduct.UnitsInStockColumn] = value;
}
}
public short UnitsOnOrder {
get {
try {
return ((short)(this[this.tableProduct.UnitsOnOrderColumn]));
}
catch (InvalidCastException e) {
throw new StrongTypingException("无法获取值,因为它是 DBNull。", e);
}
}
set {
this[this.tableProduct.UnitsOnOrderColumn] = value;
}
}
public short ReorderLevel {
get {
try {
return ((short)(this[this.tableProduct.ReorderLevelColumn]));
}
catch (InvalidCastException e) {
throw new StrongTypingException("无法获取值,因为它是 DBNull。", e);
}
}
set {
this[this.tableProduct.ReorderLevelColumn] = value;
}
}
public bool Discontinued {
get {
return ((bool)(this[this.tableProduct.DiscontinuedColumn]));
}
set {
this[this.tableProduct.DiscontinuedColumn] = value;
}
}
public bool IsSupplierIDNull() {
return this.IsNull(this.tableProduct.SupplierIDColumn);
}
public void SetSupplierIDNull() {
this[this.tableProduct.SupplierIDColumn] = System.Convert.DBNull;
}
public bool IsCategoryIDNull() {
return this.IsNull(this.tableProduct.CategoryIDColumn);
}
public void SetCategoryIDNull() {
this[this.tableProduct.CategoryIDColumn] = System.Convert.DBNull;
}
public bool IsQuantityPerUnitNull() {
return this.IsNull(this.tableProduct.QuantityPerUnitColumn);
}
public void SetQuantityPerUnitNull() {
this[this.tableProduct.QuantityPerUnitColumn] = System.Convert.DBNull;
}
public bool IsUnitPriceNull() {
return this.IsNull(this.tableProduct.UnitPriceColumn);
}
public void SetUnitPriceNull() {
this[this.tableProduct.UnitPriceColumn] = System.Convert.DBNull;
}
public bool IsUnitsInStockNull() {
return this.IsNull(this.tableProduct.UnitsInStockColumn);
}
public void SetUnitsInStockNull() {
this[this.tableProduct.UnitsInStockColumn] = System.Convert.DBNull;
}
public bool IsUnitsOnOrderNull() {
return this.IsNull(this.tableProduct.UnitsOnOrderColumn);
}
public void SetUnitsOnOrderNull() {
this[this.tableProduct.UnitsOnOrderColumn] = System.Convert.DBNull;
}
public bool IsReorderLevelNull() {
return this.IsNull(this.tableProduct.ReorderLevelColumn);
}
public void SetReorderLevelNull() {
this[this.tableProduct.ReorderLevelColumn] = System.Convert.DBNull;
}
}
[System.Diagnostics.DebuggerStepThrough()]
public class ProductRowChangeEvent : EventArgs {
private ProductRow eventRow;
private DataRowAction eventAction;
public ProductRowChangeEvent(ProductRow row, DataRowAction action) {
this.eventRow = row;
this.eventAction = action;
}
public ProductRow Row {
get {
return this.eventRow;
}
}
public DataRowAction Action {
get {
return this.eventAction;
}
}
}
}