November 16, 2010

XML結構描述定義(XSD)轉物件類別的方法(2) - 使用Xsd2Code

接續前一篇文章XML結構描述定義(XSD)轉物件類別的方法(1) - 使用xsd.exe,本篇要介紹另一個XSD轉物件類別的工具 - Xsd2Code

Xsd2Code是一個Codeplex上的Visual Studio Add-in,安裝後可直接在VS中右鍵選取*.xsd檔進行轉換成類別的動作,如下

點選Run Xsd2Code generation會出現如下畫面

 在Options標籤頁畫面中所做的設定將會影響到輸出的類別內容,以下針對小弟有測試過的幾個設定做簡單的說明
CustomUsings:新增除了預設以外會使用到的命名空間
Language:產出類別的語言,C#VB.NET
NameSpace:產出類別的命名空間
CollectionObjectType:針對XSD內設定為unbouned的元素輸出時要使用的集合型別(Arrary、List、IList, etc.)
EnableInitializeFields:初始化欄位值,例如私有變數。初始化會發生在建構式或屬性(做lazy loading/deferred loading時用)裡。
PropertyParams->AutomaticProperties:自動實作屬性,這是C# 3.0開始有支援的一個feature,如public string Name {get;set;},而不必透過私有變數來存取屬性。使用這個設定前,TargetFramework一定要設為NET30以上版本

以下為使用Xsd2Code產生的類別範例
 // ------------------------------------------------------------------------------
//  <auto-generated>
//    Generated by Xsd2Code. Version 3.4.0.38967
//    <NameSpace>Xsd2Entities</NameSpace><Collection>List</Collection><codeType>CSharp</codeType><EnableDataBinding>False</EnableDataBinding><EnableLazyLoading>False</EnableLazyLoading><TrackingChangesEnable>False</TrackingChangesEnable><GenTrackingClasses>False</GenTrackingClasses><HidePrivateFieldInIDE>False</HidePrivateFieldInIDE><EnableSummaryComment>False</EnableSummaryComment><VirtualProp>False</VirtualProp><IncludeSerializeMethod>False</IncludeSerializeMethod><UseBaseClass>False</UseBaseClass><GenBaseClass>False</GenBaseClass><GenerateCloneMethod>False</GenerateCloneMethod><GenerateDataContracts>False</GenerateDataContracts><CodeBaseTag>Net35</CodeBaseTag><SerializeMethodName>Serialize</SerializeMethodName><DeserializeMethodName>Deserialize</DeserializeMethodName><SaveToFileMethodName>SaveToFile</SaveToFileMethodName><LoadFromFileMethodName>LoadFromFile</LoadFromFileMethodName><GenerateXMLAttributes>False</GenerateXMLAttributes><EnableEncoding>False</EnableEncoding><AutomaticProperties>False</AutomaticProperties><GenerateShouldSerialize>False</GenerateShouldSerialize><DisableDebug>False</DisableDebug><PropNameSpecified>Default</PropNameSpecified><Encoder>UTF8</Encoder><CustomUsings></CustomUsings><ExcludeIncludedTypes>False</ExcludeIncludedTypes><EnableInitializeFields>True</EnableInitializeFields>
//  </auto-generated>
// ------------------------------------------------------------------------------
namespace Xsd2Entities {
    using System;
    using System.Diagnostics;
    using System.Xml.Serialization;
    using System.Collections;
    using System.Xml.Schema;
    using System.ComponentModel;
    using System.Collections.Generic;


    public partial class BooksForm {
    
        private List<BookForm> bookField;
    
        public BooksForm() {
            this.bookField = new List<BookForm>();
        }
    
        public List<BookForm> book {
            get {
                return this.bookField;
            }
            set {
                this.bookField = value;
            }
        }
    }

    public partial class BookForm {
    
        private string authorField;
    
        private string titleField;
    
        private string genreField;
    
        private float priceField;
    
        private System.DateTime pub_dateField;
    
        private string reviewField;
    
        private string idField;
    
        public string author {
            get {
                return this.authorField;
            }
            set {
                this.authorField = value;
            }
        }
    
        public string title {
            get {
                return this.titleField;
            }
            set {
                this.titleField = value;
            }
        }
    
        public string genre {
            get {
                return this.genreField;
            }
            set {
                this.genreField = value;
            }
        }
    
        public float price {
            get {
                return this.priceField;
            }
            set {
                this.priceField = value;
            }
        }
    
        public System.DateTime pub_date {
            get {
                return this.pub_dateField;
            }
            set {
                this.pub_dateField = value;
            }
        }
    
        public string review {
            get {
                return this.reviewField;
            }
            set {
                this.reviewField = value;
            }
        }
    
        public string id {
            get {
                return this.idField;
            }
            set {
                this.idField = value;
            }
        }
    }
}

No comments: