Page tree

Introduction

The code generation parameters are accessible from the xsd2code interface. They are in the form of a PropertyGrid. All parameters are available through this interface.

xsd2code propertygrid settings

Changing a value automatically triggers code generation. This option can be disabled if required:

xsd2code live code

If you want to generate your code using a command script, it is possible to automatically retrieve the parameters in the Command Line property.

xsd2code command line

Language

Determines the language to be used to generate the code. CSharp or VB.

NameSpace

Allows you to specify the NameSpace.

TargetFramework

The target framework must match the one of your project. xsd2code++ detects it on the first call. You can then change it if your project changes too. However this option allows xsd2code++ to enable or disable options depending on the target framework. For example with TargetFramework 2.0 which could be used to maintain an old application for example, the automatic properties will not be available. 


Framework 2.0Framework 3.x to 4.x & .Net5
Automatic properties(error)(tick)
WCF Attributes(error)(tick)
JSon attributes(error)(tick)
INotifyPropertyChanged(error)(tick)

ExcludeImportedType

This setting allows you to ignore all elements from an imported schema. Only complex or simple elements from the schema itself will be taken into account. Otherwise, and by default the schema parser will browse all the included or imported schemas. When you want to generate all the classes of a schema set, you just have to generate the code from the root schema.

The schema below (PurchaseOrder.xsd) contains an imported schema (Product.xsd). The schema Product.xsd contains the definition of the Product element.

PurchaseOrder.xsd
<?xml version="1.0"?>
<xsd:schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns:p="http://xsd2code.com/product">
  <xsd:import namespace="http://xsd2code.com/product" schemaLocation="product.xsd"/>
  <xsd:complexType name="PurchaseOrderType">
    <xsd:sequence>
      <xsd:element minOccurs="1" maxOccurs="unbounded" name="Products" type="p:Product" />
      <xsd:element minOccurs="1" maxOccurs="unbounded" name="ShipTo" type="USAddress" />
      <xsd:element name="BillTo" type="USAddress" />
      <xsd:element name="OrderDate" type="xsd:string" />
    </xsd:sequence>
  </xsd:complexType>
  <xsd:complexType name="USAddress">
    <xsd:sequence>
      <xsd:element name="name" type="xsd:string" />
      <xsd:element name="street" type="xsd:string" />
      <xsd:element name="city" type="xsd:string" />
      <xsd:element name="state" type="xsd:string" />
      <xsd:element name="zip" type="xsd:int" />
      <xsd:element name="country" type="xsd:string" />
    </xsd:sequence>
  </xsd:complexType>
  <xsd:element name="PurchaseOrder" type="PurchaseOrderType" />
</xsd:schema>


Product.xsd
<?xml version="1.0"?>
<xsd:schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            targetNamespace="http://xsd2code.com/product">
  <xsd:complexType name="Product">
    <xsd:sequence>
      <xsd:element name="SKU" type="xsd:string" />
      <xsd:element name="Name" type="xsd:string" />
    </xsd:sequence>
  </xsd:complexType>
</xsd:schema>

The target framework must match the one of your project.

ExcludeImportedType

In case the schemas are externalized (URL) they will be automatically downloaded and analyzed by the parser.

ExpandNestedAttributeGroup

GenerateUnusedComplexType

This option forces the generator to include the classes and properties of orphan complex elements. That is to say those which are not attached to the root element.

In the sample below, the complex type Product is never used in the root element tree. It is an "orphan" complex type in the sense that it is not used. By default xsd2code++ ignores it. It is therefore possible to force the generator to integrate them.

PurchaseOrderType.xsd
<?xml version="1.0"?>
<xsd:schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns:p="http://xsd2code.com/product">
  <xsd:import namespace="http://xsd2code.com/product" schemaLocation="product.xsd"/>
  <xsd:complexType name="PurchaseOrderType">
    <xsd:sequence>
      <xsd:element minOccurs="1" maxOccurs="unbounded" name="ShipTo" type="USAddress" />
      <xsd:element name="BillTo" type="USAddress" />
      <xsd:element name="OrderDate" type="xsd:string" />
    </xsd:sequence>
  </xsd:complexType>
  <xsd:complexType name="USAddress">
    <xsd:sequence>
      <xsd:element name="name" type="xsd:string" />
      <xsd:element name="street" type="xsd:string" />
      <xsd:element name="city" type="xsd:string" />
      <xsd:element name="state" type="xsd:string" />
      <xsd:element name="zip" type="xsd:int" />
      <xsd:element name="country" type="xsd:string" />
    </xsd:sequence>
  </xsd:complexType>
  <xsd:element name="PurchaseOrder" type="PurchaseOrderType" />

  <xsd:complexType name="Product">
    <xsd:sequence>
      <xsd:element name="SKU" type="xsd:string" />
      <xsd:element name="Name" type="xsd:string" />
    </xsd:sequence>
  </xsd:complexType>
  
</xsd:schema>

Here is the code generated by default, i.e. with the option GenerateUnusedComplexType=false

GenerateUnusedComplexType=false (default)
namespace ConsoleDotNetCoreSample
{
    using System;
    using System.Diagnostics;
    using System.Xml.Serialization;
    using System.Collections;
    using System.Xml.Schema;
    using System.ComponentModel;
    using System.Xml;
    using System.Collections.Generic;
    
    
    public partial class PurchaseOrderType
    {
        public List<USAddress> ShipTo { get; set; }
        public USAddress BillTo { get; set; }
        public string OrderDate { get; set; }
        
        public PurchaseOrderType()
        {
            BillTo = new USAddress();
            ShipTo = new List<USAddress>();
        }
    }
    
    public partial class USAddress
    {
        public string name { get; set; }
        public string street { get; set; }
        public string city { get; set; }
        public string state { get; set; }
        public int zip { get; set; }
        public string country { get; set; }
    }
}

Here is the code generated with the option GenerateUnusedComplexType=true

GenerateUnusedComplexType=true
namespace ConsoleDotNetCoreSample
{
    using System;
    using System.Diagnostics;
    using System.Xml.Serialization;
    using System.Collections;
    using System.Xml.Schema;
    using System.ComponentModel;
    using System.Xml;
    using System.Collections.Generic;
    
    
    public partial class PurchaseOrderType
    {
        public List<USAddress> ShipTo { get; set; }
        public USAddress BillTo { get; set; }
        public string OrderDate { get; set; }
        
        public PurchaseOrderType()
        {
            BillTo = new USAddress();
            ShipTo = new List<USAddress>();
        }
    }
    
    public partial class USAddress
    {
        public string name { get; set; }
        public string street { get; set; }
        public string city { get; set; }
        public string state { get; set; }
        public int zip { get; set; }
        public string country { get; set; }
    }
    
    public partial class Product
    {
        public string SKU { get; set; }
        public string Name { get; set; }
    }
}

GenerateUnusedSimpleType

This option is used to generate the classes and properties of single orphan elements. I.e. those which are not attached to the root element. This is the same principle as the GenerateUnusedComplexType option.

CustomUsing

The custom using parameter allows you to add additional directives.

CustomUsing

GenerateCloneMethod

This parameter makes it possible to generate in each class a Clone() method allowing the cloning of an instance.

Clone()
        public virtual PurchaseOrderType Clone()
        {
            return ((PurchaseOrderType)(MemberwiseClone()));
        }

InitializeFields

This parameter définit la manière dont les object sont initialisés.

InitializeFields.All (Default)

Adds a constructor to your classes including the creation of instances of complex objects or collections.

InitializeFields.All

InitializeFields.AllExceptOptional

If in your schema an element is marked as optional, the instance of this element will not be included in the constructor. An optional element is considered optional when it has a minOccurs="0". In the example below, the BillTo element is considered as an option because the minimum occurrence is 0.

  <xsd:complexType name="PurchaseOrderType">
    <xsd:sequence>
      <xsd:element name="ShipTo" type="USAddress" maxOccurs="unbounded"/>
      <xsd:element name="BillTo" type="USAddress" minOccurs="0" />
      <xsd:element name="OrderDate" type="xsd:string" />
    </xsd:sequence>
  </xsd:complexType>

xsd2code InitializeFields.AllExceptOptional

InitializeFields.None

No object instance will be created.

Miscellaneous

CacheExternalSchames

When your XSD schema contains schemas imported from URL, xsd2code++ automatically downloads them so that it can analyze them and generate the corresponding classes. To optimize response time, it is possible to place the schemas in a local cache. The cache is physically located on your hard drive at the following folder.

%appdata%\xsd2code\


CleanupCode

This parameter is used to clean up the code and minify it. This allows you to reduce any namespace in particular.
For example,

    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.8.3752.0")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://www.google.com/schemas/sitemap-news/0.9")]
    [System.Xml.Serialization.XmlRootAttribute(Namespace="http://www.google.com/schemas/sitemap-news/0.9", IsNullable=false)]
    public partial class news
    { ... }

Becomes

    [GeneratedCode("System.Xml", "4.8.3752.0")]
    [Serializable]
    [DebuggerStepThrough]
    [DesignerCategory("code")]
    [XmlType(AnonymousType=true, Namespace="http://www.google.com/schemas/sitemap-news/0.9")]
    [XmlRoot(Namespace="http://www.google.com/schemas/sitemap-news/0.9", IsNullable=false)]
    public partial class news
    { ... }


EnableDebug

By default the generated classes are decorated with the [DebuggerStepThrough] attribute which prevents the debugger from entering into the generated code. However, if you want to disable this option, set EnableDebug to true.


EnableSummaryComment

This option allows you to retrieve element annotations for xsd schemas or description for JSon schemas into genrated code :

      <xsd:element name="name" type="xsd:string" >
          <xsd:annotation>
             <xsd:documentation>
                  Name of the customer that will be used for invoicing.
             </xsd:documentation>
          </xsd:annotation>
      </xsd:element>


        /// <summary>
        /// Name of the customer that will be used for invoicing.
        /// </summary>
        public string name
        {
            ...
        }


EnableWarning

By default the generated classes include the #pragma warning disable directive to avoid generating warnings when compiling your project.


HidePrivateIntelliSense

This option adds on all private members the attribute EditorBrowsable(EditorBrowsableState.Never)] which avoids listing them in IntelliSense of the Visual Studio Code Editor.

  • No labels