posts - 81, comments - 262, trackbacks - 0

Authoring Easy XSDs from XML


Visual Studio ships with a powerful tool, xsd.exe, which can easy and quickly generate XSD's and typed datasets from references. I found this to be particularly useful when I need to utilize a typed data set. I can write a sample of the xml I would like to generate, and then create the XSD and class off of that.

For example, create cats.xml like so:

<cats>

    <cat>Cat 1</cat>

    <cat>Cat 2</cat>

</cats>

 

Start visual studio command prompt and navigate to the directory. Then issue:

xsd cats.xml

xsd cats.xsd /c

 

Two files have now been created, cats.xsd and cats.cs. the XSD can be used as a reference for a third party to create services on top of. The class, a typed data set can be used to package data and easily write it to xml with .GetXML().

 

<?xml version="1.0" encoding="utf-8"?>

<xs:schema id="cats" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">

<xs:element name="cats" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">

<xs:complexType>

<xs:choice minOccurs="0" maxOccurs="unbounded">

<xs:element name="cat" nillable="true">

<xs:complexType>

<xs:simpleContent msdata:ColumnName="cat_Text" msdata:Ordinal="0">

<xs:extension base="xs:string">

</xs:extension>

</xs:simpleContent>

</xs:complexType>

</xs:element>

</xs:choice>

</xs:complexType>

</xs:element>

</xs:schema>

 

//------------------------------------------------------------------------------

// <auto-generated>

// This code was generated by a tool.

// Runtime Version:2.0.50727.1434

//

// Changes to this file may cause incorrect behavior and will be lost if

// the code is regenerated.

// </auto-generated>

//------------------------------------------------------------------------------

 

using System.Xml.Serialization;

 

//

// This source code was auto-generated by xsd, Version=2.0.50727.1432.

//

 

 

/// <remarks/>

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.1432")]

[System.SerializableAttribute()]

[System.Diagnostics.DebuggerStepThroughAttribute()]

[System.ComponentModel.DesignerCategoryAttribute("code")]

[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]

[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]

public partial class cats {

 

private catsCat[] itemsField;

 

/// <remarks/>

[System.Xml.Serialization.XmlElementAttribute("cat", Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=true)]

public catsCat[] Items {

get {

return this.itemsField;

}

set {

this.itemsField = value;

}

}

}

 

/// <remarks/>

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.1432")]

[System.SerializableAttribute()]

[System.Diagnostics.DebuggerStepThroughAttribute()]

[System.ComponentModel.DesignerCategoryAttribute("code")]

[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]

public partial class catsCat {

 

private string valueField;

 

/// <remarks/>

[System.Xml.Serialization.XmlTextAttribute()]

public string Value {

get {

return this.valueField;

}

set {

this.valueField = value;

}

}

}

 

 

 

Microsoft (R) Xml Schemas/DataTypes support utility

[Microsoft (R) .NET Framework, Version 2.0.50727.1432]

Copyright (C) Microsoft Corporation. All rights reserved.

 

xsd.exe -

Utility to generate schema or class files from given source.

 

xsd.exe <schema>.xsd /classes|dataset [/e:] [/l:] [/n:] [/o:] [/s] [/uri:]

xsd.exe <assembly>.dll|.exe [/outputdir:] [/type: [...]]

xsd.exe <instance>.xml [/outputdir:]

xsd.exe <schema>.xdr [/outputdir:]

 

- OPTIONS -

 

/classes

Generate classes for this schema. Short form is '/c'.

 

/dataset

Generate sub-classed DataSet for this schema. Short form is '/d'.

 

/enableLinqDataSet

Generate LINQ-enabled sub-classed Dataset for the schemas provided. Short form is '/eld'.

 

/element:<element>

Element from schema to process. Short form is '/e:'.

 

/fields

Generate fields instead of properties. Short form is '/f'.

 

/order

Generate explicit order identifiers on all particle members.

 

/enableDataBinding

Implement INotifyPropertyChanged interface on all generated types

to enable data binding. Short form is '/edb'.

 

/language:<language>

The language to use for the generated code. Choose from 'CS', 'VB', 'JS',

'VJS', 'CPP' or provide a fully-qualified name for a class implementing

System.CodeDom.Compiler.CodeDomProvider. The default language

is 'CS' (CSharp). Short form is '/l:'.

 

/namespace:<namespace>

The namespace for generated class files. The default namespace

is the global namespace. Short form is '/n:'.

 

/nologo

Suppresses the banner.

 

/out:<directoryName>

The output directory to create files in. The default

is the current directory. Short form is '/o:'.

 

/type:<type>

Type from assembly to generate schema for. Multiple types may be provided.

If no types are provided, then schemas for all types in an assembly

are generated. Short form is '/t:'.

 

/uri:<uri>

Uri of elements from schema to process. Short form is '/u:'.

 

- ADVANCED -

 

/parameters:<file>

Read command-line options from the specified xml file. Short form is '/p:'.

 

- ARGUMENTS -

<schema>.xsd Name of a schema containing elements to import.

<assembly>.dll|exe Name of an assembly containing types to generate schema for.

<instance>.xml Name of an xml file to infer xsd schema from.

<schema>.xdr Name of an xdr schema to convert to xsd.

Multiple file arguments of the same type may be provided.


Print | posted on Saturday, May 17, 2008 5:02 PM | Filed Under [ Web Programming ]

Powered by:
Powered By Subtext Powered By ASP.NET