Project Description
Xsd2Code is a CSharp or Visual Basic Business Entity class Generator from XSD schema.

What Xsd2Code can do ?

  • 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. (new in 2.8.4).
  • Can generate WCF attributes (DataContract/DataMember). (new in 2.8.4).
  • Support nillable type.
  • Mask private field in IDE (use EditorBrowsableState.Never attribute).
  • Generate object allocation in constructor.
  • Implement INotifyPropertyChanged for enable DataBinding for wpf or Silverlight.
  • Improves productivity with visual studio add-in.
  • Generate summary documentation from xsd annotation.
  • Check if the new and old values int setter are the same before raising property changed event.
  • backup options generation in cs or vb header.
  • Generate CS, VB or CPP code.
  • Serialize/deserilize object.
  • Save into file and load from file.
  • Include Xsd2CodeCustomTool.

How to use it ?

Xsd2Code is an AddIn for visual studio 2008.
Right clic on xsd schema in solution explorer, choose options and generate code.

AddinMenu.jpg

Addin.jpg

Xsd2Code has a CustomTool which allows automatic generation when schema is modified.
CustomTool.jpg

Enable databinding.

      <xs:element name="show" type="xs:boolean" nillable="true"/>

Result :
        [System.Xml.Serialization.XmlElementAttribute(IsNullable=true)]
        public System.Nullable<bool> show {
            get {
                return this.showField;
            }
            set {
                if ((showField.Equals(value) != true)) {
                    this.showField = value;
                    OnPropertyChanged("show");
                }
            }
        }

        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
        
        private void 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.

Collection.jpg

<xs:element name="Product">
    <xs:complexType>
      <xs:sequence maxOccurs="unbounded">
        <xs:element name="ProductName" type="xs:string" />
        <xs:element name="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 {
                return this.productNameField;
            }
            set {
                if ((this.productNameField != null)) {
                    if ((productNameField.Equals(value) != true)) {
                        this.productNameField = value;
                        OnPropertyChanged("ProductName");
                    }
                }
                else {
                    this.productNameField = value;
                    OnPropertyChanged("ProductName");
                }
            }
        }

Serialize/Deserialize method.

        /// <summary>
        /// Serializes current Actors object into an XML document
        /// </summary>
        // <returns>string XML value</returns>
        public virtual string Serialize()
        {
            System.Xml.Serialization.XmlSerializer xmlSerializer = 
                 new System.Xml.Serialization.XmlSerializer(this.GetType());
            System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();
            xmlSerializer.Serialize(memoryStream, this);
            memoryStream.Seek(0, System.IO.SeekOrigin.Begin);
            System.IO.StreamReader streamReader = new System.IO.StreamReader(memoryStream);
            return streamReader.ReadToEnd();
        }

        /// <summary>
        /// Deserializes workflow markup into an Actors object
        /// </summary>
        // <param name="xml">string workflow markup to deserialize</param>
        // <param name="obj">Output Actors object</param>
        // <param name="exception">output Exception value if deserialize failed</param>
        // <returns>true if this XmlSerializer can deserialize the object; otherwise, false</returns>
        public static bool Deserialize(string xml, out Actors obj, out System.Exception exception)
        {
            exception = null;
            obj = null;
            try
            {
                System.IO.StringReader stringReader = new System.IO.StringReader(xml);
                System.Xml.XmlTextReader xmlTextReader = 
                       new System.Xml.XmlTextReader(stringReader);
                System.Xml.Serialization.XmlSerializer xmlSerializer = 
                      new System.Xml.Serialization.XmlSerializer(typeof(Actors));
                obj = ((Actors)(xmlSerializer.Deserialize(xmlTextReader)));
                return true;
            }
            catch (System.Exception e)
            {
                exception = e;
                return false;
            }
        }

SaveToFile and LoadFromFile method.

        /// <summary>
        /// Serializes current Actors 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>
        public virtual bool SaveToFile(string fileName, out System.Exception exception)
        {
            exception = null;
            try
            {
                string xmlString = Serialize();
                System.IO.FileInfo xmlFile = new System.IO.FileInfo(fileName);
                System.IO.StreamWriter streamWriter = xmlFile.CreateText();
                streamWriter.WriteLine(xmlString);
                streamWriter.Close();
                return true;
            }
            catch (System.Exception e)
            {
                exception = e;
                return false;
            }
        }

        /// <summary>
        /// Deserializes workflow markup from file into an Actors object
        /// </summary>
        // <param name="xml">string workflow markup to deserialize</param>
        // <param name="obj">Output Actors object</param>
        // <param name="exception">output Exception value if deserialize failed</param>
        // <returns>true if this XmlSerializer can deserialize the object; otherwise, false</returns>
        public static bool LoadFromFile(string fileName, out Actors obj, out System.Exception exception)
        {
            exception = null;
            obj = null;
            try
            {
                System.IO.FileStream file = new System.IO.FileStream(fileName, FileMode.Open, FileAccess.Read);
                System.IO.StreamReader sr = new System.IO.StreamReader(file);
                string xmlString = sr.ReadToEnd();
                sr.Close();
                file.Close();
                return Deserialize(xmlString, out obj, out exception);
            }
            catch (System.Exception e)
            {
                exception = e;
                return false;
            }
        }

Default value.

<xs:attribute name="nationality" type="xs:string" default="US">

Result :
        /// <summary>
        /// .ctor class constructor
        /// </summary>
        public Actor() {
            this.nationalityField = "US";
        }

Code xml comment.

 <xs:element name="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>
        public string 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;
    ...

For exemple, full generated csharp file : dvd.cs
XmlSchema exemple : dvd.xsd
XmlSchema exemple : Actor.xsd

This project is based on the work originally done by Daniel Cazzulino in 2004
You can find the original project here :
http://msdn.microsoft.com/en-us/library/aa302301.aspx

From pascal cabanel.
My blog (French only) : http://pcabanel.over-blog.com/
Last edited Jun 22 at 8:20 AM by pcabanel, version 66

 

Want to leave feedback?
Please use Discussions or Reviews instead.

Archived page comments (5)

Updating...
© 2006-2009 Microsoft | About CodePlex | Privacy Statement | Terms of Use | Code of Conduct | Version 2009.6.1.15196