Quantcast
Channel: xsd2Code community edition .net class generator from XSD schema
Viewing all articles
Browse latest Browse all 543

Updated Wiki: Home

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

If you feel that the work I provide to the community is worth it,
you can donate by using the Donate button below. Thank you.
Donate

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.
  • Can generate WCF attributes (DataContract/DataMember).
  • Support nillable type.
  • 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.
  • Serialize/deserilize to string or from object.
  • Save and load Xml document into isolated file storage for silverlight app (new in 3.0).
  • Read an write xml with UTF-8/32, ASCII, Unicode, Default. (new in 3.4).
  • Generate CS, VB code.
  • Save into file and load from file.
  • Include Xsd2CodeCustomTool.
  • Mask private field in IDE (use EditorBrowsableState.Never attribute).

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

propertyGrid.png

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");
                }
            }
        }

Lazy loading pattern.

        [System.Xml.Serialization.XmlElementAttribute("Dvds")]
        public List<dvd> Dvds {
            get {
                if ((this.dvdsField == null)) {
                    this.dvdsField = new List<dvd>();
                }
                return this.dvdsField;
            }
            set {
                this.dvdsField = value;
            }
        }

Serialize/Deserialize method.

/// <summary>
        /// Serializes current EntityBase object into an XML document
        /// </summary>
        // <returns>string XML value</returns>
        public virtual string 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>
        public static bool Deserialize(string xml, out T obj, out System.Exception exception) {
            exception = null;
            obj = default(T);
            try {
                obj = Deserialize(xml);
                return true;
            }
            catch (System.Exception ex) {
                exception = ex;
                return false;
            }
        }
        
        public static bool Deserialize(string xml, out T obj) {
            System.Exception exception = null;
            return Deserialize(xml, out obj, out exception);
        }
        
        public static 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>
        public virtual bool SaveToFile(string fileName, out System.Exception exception) {
            exception = null;
            try {
                SaveToFile(fileName);
                return true;
            }
            catch (System.Exception e) {
                exception = e;
                return false;
            }
        }
        
        public virtual void 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>
        public static bool LoadFromFile(string fileName, out T obj, out System.Exception exception) {
            exception = null;
            obj = default(T);
            try {
                obj = LoadFromFile(fileName);
                return true;
            }
            catch (System.Exception ex) {
                exception = ex;
                return false;
            }
        }
        
        public static bool LoadFromFile(string fileName, out T obj) {
            System.Exception exception = null;
            return LoadFromFile(fileName, out obj, out exception);
        }
        
        public static 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: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;
    ...

From pascal cabanel.
About me : http://fr.viadeo.com/profile/0022904hc8vzkt3t/en/

Viewing all articles
Browse latest Browse all 543

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>