Home  /  Blog  /  Documentation  /  Code Generator   /  Settings

Settings

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.

Image showing the xsd2code interface

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

Image showing the button to generate and the option to open the code after generation

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

Image showing the command lind property in Tools

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.0 Framework 3.x to 4.x & .Net5
Automatic properties
WCF Attributes
JSon attributes
INotifyPropertyChanged

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.

<?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>
<?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>
Image showing and explaining that the product class with be ignored with the ExcludeImportedTypes=true


The default imported product class of schema Product.xsd. With the ExcludeImportedTypes=true, this class will be ignored.

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.

<?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

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

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.

Image showing the custom using parametres and its effects

GenerateCloneMethod

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

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

InitializeFields

This setting defines the way in which objects are initialized

InitializeFields.All (Default)

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

Image showing the InitializeFields.All on default and it's effects

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>
Image showing the InitializeFilds.AllExceptOptional and its effects

InitializeFields.None

No object instance will be created.

Terms & privacy

Terms of uses · Privacy policy 

Contact

CodeNGine Technologies, 19 Avenue, de la gare, 34770 Gigean, France. SIRET: 85257252800010, VAT : FR 62852572528
codengine.technologies@xsd2code.com

About the company

CodeNGine Technologies is software company based in the South of France that provides an advanced productivity code generation tool to developers using XML and JSON


Copyright © 2021 CodeNGine Technologies