Project Description
Xsd2Code community edition is a CSharp or Visual Basic Business Entity class Generator from XSD schema.
What Xsd2Code community edition can do ?
- Generate CS, VB code from xsd schema.
- Generate partial class.
- Support generic and custom collection (List<T>, ObservableCollection<T>, MyCustomCollection<T>).
- Support automatic properties when no special get or set is required.
- Can generate WCF attributes (DataContract/DataMember).
- Implement INotifyPropertyChanged for enable DataBinding.
- Generate summary documentation from xsd annotation.
- Save and load Xml document into isolated file storage for silverlight app.
- Read an write xml with UTF-8/32, ASCII, Unicode, Default.
- Save and load from XML file.
xsd2code is available as dual-licensing.
- xsd2code++ (www.xsd2code.com)
Paid version with additional features for developers and business customers working on commercial projects. - xsd2code community edition (here on codeplex)
Free version for individual developers working on personal or open source projects.
How to use it ?
Xsd2Code community edition is an AddIn for visual studio 2008.
Right clic on xsd schema in solution explorer, choose options and generate code.
Enable databinding.
<xs:elementname="show"type="xs:boolean"nillable="true"/>
Result :
[System.Xml.Serialization.XmlElementAttribute(IsNullable=true)]public System.Nullable<bool> show {get {returnthis.showField; }set {if ((showField.Equals(value) != true)) {this.showField = value; OnPropertyChanged("show"); } } }publicevent System.ComponentModel.PropertyChangedEventHandler PropertyChanged;privatevoid OnPropertyChanged(string info) { PropertyChangedEventHandler handler = PropertyChanged;if (handler != null) { handler(this, new PropertyChangedEventArgs(info)); } }
Hide private field in Visual studio.
[EditorBrowsable(EditorBrowsableState.Never)]private List <dvds> dvdsField;
Generic collection.
<xs:elementname="Product">
<xs:complexType>
<xs:sequencemaxOccurs="unbounded">
<xs:elementname="ProductName"type="xs:string"/>
<xs:elementname="ProductVersion"type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
Result :
///<summary>/// .ctor class constructor///</summary>public Product() {if ((this.productVersionField == null)) {this.productVersionField = new List<System.String>(); }if ((this.productNameField == null)) {this.productNameField = new List<System.String>(); } }///<remarks/> [System.Xml.Serialization.XmlElementAttribute("ProductName")]public List<System.String> ProductName {get {returnthis.productNameField; }set {if ((this.productNameField != null)) {if ((productNameField.Equals(value) != true)) {this.productNameField = value; OnPropertyChanged("ProductName"); } }else {this.productNameField = value; OnPropertyChanged("ProductName"); } } }
Lazy loading pattern.
[System.Xml.Serialization.XmlElementAttribute("Dvds")]public List<dvd> Dvds {get {if ((this.dvdsField == null)) {this.dvdsField = new List<dvd>(); }returnthis.dvdsField; }set {this.dvdsField = value; } }
Serialize/Deserialize XML.
///<summary>/// Serializes current EntityBase object into an XML document///</summary>// <returns>string XML value</returns>publicvirtualstring Serialize() { System.IO.StreamReader streamReader = null; System.IO.MemoryStream memoryStream = null;try { memoryStream = new System.IO.MemoryStream(); Serializer.Serialize(memoryStream, this); memoryStream.Seek(0, System.IO.SeekOrigin.Begin); streamReader = new System.IO.StreamReader(memoryStream);return streamReader.ReadToEnd(); }finally {if (streamReader != null) { streamReader.Dispose(); }if (memoryStream != null) { memoryStream.Dispose(); } } }///<summary>/// Deserializes workflow markup into an EntityBase object///</summary>// <param name="xml">string workflow markup to deserialize</param>// <param name="obj">Output EntityBase object</param>// <param name="exception">output Exception value if deserialize failed</param>// <returns>true if this XmlSerializer can deserialize the object; otherwise, false</returns>publicstaticbool Deserialize(string xml, out T obj, out System.Exception exception) { exception = null; obj = default(T);try { obj = Deserialize(xml);returntrue; }catch (System.Exception ex) { exception = ex;returnfalse; } }publicstaticbool Deserialize(string xml, out T obj) { System.Exception exception = null;return Deserialize(xml, out obj, out exception); }publicstatic T Deserialize(string xml) { System.IO.StringReader stringReader = null;try { stringReader = new System.IO.StringReader(xml);return ((T)(Serializer.Deserialize(System.Xml.XmlReader.Create(stringReader)))); }finally {if (stringReader != null) { stringReader.Dispose(); } } }
SaveToFile and LoadFromFile method.
///<summary>/// Serializes current EntityBase object into file///</summary>// <param name="fileName">full path of outupt xml file</param>// <param name="exception">output Exception value if failed</param>// <returns>true if can serialize and save into file; otherwise, false</returns>publicvirtualbool SaveToFile(string fileName, out System.Exception exception) { exception = null;try { SaveToFile(fileName);returntrue; }catch (System.Exception e) { exception = e;returnfalse; } }publicvirtualvoid SaveToFile(string fileName) { System.IO.StreamWriter streamWriter = null;try {string xmlString = Serialize(); System.IO.FileInfo xmlFile = new System.IO.FileInfo(fileName); streamWriter = xmlFile.CreateText(); streamWriter.WriteLine(xmlString); streamWriter.Close(); }finally {if (streamWriter != null) { streamWriter.Dispose(); } } }///<summary>/// Deserializes workflow markup from file into an EntityBase object///</summary>// <param name="xml">string workflow markup to deserialize</param>// <param name="obj">Output EntityBase object</param>// <param name="exception">output Exception value if deserialize failed</param>// <returns>true if this XmlSerializer can deserialize the object; otherwise, false</returns>publicstaticbool LoadFromFile(string fileName, out T obj, out System.Exception exception) { exception = null; obj = default(T);try { obj = LoadFromFile(fileName);returntrue; }catch (System.Exception ex) { exception = ex;returnfalse; } }publicstaticbool LoadFromFile(string fileName, out T obj) { System.Exception exception = null;return LoadFromFile(fileName, out obj, out exception); }publicstatic T LoadFromFile(string fileName) { System.IO.FileStream file = null; System.IO.StreamReader sr = null;try { file = new System.IO.FileStream(fileName, FileMode.Open, FileAccess.Read); sr = new System.IO.StreamReader(file);string xmlString = sr.ReadToEnd(); sr.Close(); file.Close();return Deserialize(xmlString); }finally {if (file != null) { file.Dispose(); }if (sr != null) { sr.Dispose(); } } }
Default value.
<xs:attributename="nationality"type="xs:string"default="US">
Result :
///<summary>/// .ctor class constructor///</summary>public Actor() {this.nationalityField = "US"; }
Code xml comment.
<xs:elementname="firstname"type="xs:string"><xs:annotation><xs:documentation> Gets or sets the firstname of the actor</xs:documentation></xs:annotation></xs:element>
Result :
///<summary>/// Gets or sets the firstname of the actor///</summary>publicstring firstname {get;set;}
Backup options generation in cs or vb header
// ------------------------------------------------------------------------------// <auto-generated>// Generated by Xsd2Code. Version 2.1.3148.17485// <NameSpace>XSD2Code.Test</NameSpace><Collection>List</Collection>...// <auto-generated>// ------------------------------------------------------------------------------namespace XSD2Code.Test {using System;using System.Diagnostics; ...