简体   繁体   中英

xsd2code++ restrictions not generated

I tried to generate C#-Classes from XSD using XSD2Code++ (V4.2.0.31, licensed version). The Code generation is working, but it is omitting the restrictions.

XSD:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name ="Product">
        <xs:complexType>
            <xs:sequence maxOccurs="3">
                <xs:element name="Header" minOccurs="1" maxOccurs="1"/>
                <xs:element name="Body"/>
                <xs:element name ="value2" maxOccurs="1">
                    <xs:simpleType>
                         <xs:restriction base="xs:string">
                             <xs:minLength value="20" />
                             <xs:maxLength value="22" />
                         </xs:restriction>
                    </xs:simpleType>
                </xs:element>
                <xs:element name ="value4" maxOccurs="1">
                    <xs:simpleType>
                        <xs:restriction base="xs:int">
                            <xs:minInclusive value="5" />
                            <xs:maxInclusive value="6" />
                        </xs:restriction>
                    </xs:simpleType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

And this code was generated, the restrictions are missing. I expected to see them in the setters.

// ------------------------------------------------------------------------------
//  <auto-generated>
//    Generated by Xsd2Code++. Version 4.2.0.31
//    <NameSpace>xsd2code_Probierprojekt</NameSpace><Collection>List</Collection><codeType>CSharp</codeType><EnableDataBinding>False</EnableDataBinding><GenerateCloneMethod>False</GenerateCloneMethod><GenerateDataContracts>False</GenerateDataContracts><DataMemberNameArg>OnlyIfDifferent</DataMemberNameArg><DataMemberOnXmlIgnore>False</DataMemberOnXmlIgnore><CodeBaseTag>Net40</CodeBaseTag><InitializeFields>All</InitializeFields><GenerateUnusedComplexTypes>False</GenerateUnusedComplexTypes><GenerateUnusedSimpleTypes>False</GenerateUnusedSimpleTypes><GenerateXMLAttributes>True</GenerateXMLAttributes><OrderXMLAttrib>False</OrderXMLAttrib><EnableLazyLoading>False</EnableLazyLoading><VirtualProp>False</VirtualProp><PascalCase>False</PascalCase><AutomaticProperties>False</AutomaticProperties><PropNameSpecified>None</PropNameSpecified><PrivateFieldName>StartWithUnderscore</PrivateFieldName><PrivateFieldNamePrefix></PrivateFieldNamePrefix><EnableRestriction>True</EnableRestriction><RestrictionMaxLenght>True</RestrictionMaxLenght><RestrictionRegEx>True</RestrictionRegEx><RestrictionRange>True</RestrictionRange><ValidateProperty>True</ValidateProperty><ClassNamePrefix></ClassNamePrefix><ClassLevel>Public</ClassLevel><PartialClass>True</PartialClass><ClassesInSeparateFiles>False</ClassesInSeparateFiles><ClassesInSeparateFilesDir></ClassesInSeparateFilesDir><TrackingChangesEnable>False</TrackingChangesEnable><GenTrackingClasses>False</GenTrackingClasses><HidePrivateFieldInIDE>False</HidePrivateFieldInIDE><EnableSummaryComment>False</EnableSummaryComment><EnableAppInfoSettings>False</EnableAppInfoSettings><EnableExternalSchemasCache>False</EnableExternalSchemasCache><EnableDebug>False</EnableDebug><EnableWarn>False</EnableWarn><ExcludeImportedTypes>False</ExcludeImportedTypes><ExpandNesteadAttributeGroup>False</ExpandNesteadAttributeGroup><CleanupCode>False</CleanupCode><EnableXmlSerialization>True</EnableXmlSerialization><SerializeMethodName>Serialize</SerializeMethodName><DeserializeMethodName>Deserialize</DeserializeMethodName><SaveToFileMethodName>SaveToFile</SaveToFileMethodName><LoadFromFileMethodName>LoadFromFile</LoadFromFileMethodName><EnableEncoding>False</EnableEncoding><EnableXMLIndent>False</EnableXMLIndent><Encoder>UTF8</Encoder><Serializer>XmlSerializer</Serializer><sspNullable>False</sspNullable><sspString>False</sspString><sspCollection>False</sspCollection><sspComplexType>False</sspComplexType><sspSimpleType>False</sspSimpleType><sspEnumType>False</sspEnumType><BaseClassName>EntityBase</BaseClassName><UseBaseClass>False</UseBaseClass><GenBaseClass>False</GenBaseClass><CustomUsings></CustomUsings><AttributesToExlude></AttributesToExlude>
//  </auto-generated>
// ------------------------------------------------------------------------------
#pragma warning disable

namespace xsd2code_Probierprojekt
{
using System;
using System.Diagnostics;
using System.Xml.Serialization;
using System.Collections;
using System.Xml.Schema;
using System.ComponentModel;
using System.IO;
using System.Text;
using System.ComponentModel.DataAnnotations;
using System.Xml;
using System.Collections.Generic;


[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.34209")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class Product
{     
    private List<object> _header;
    private List<object> _body;
    private List<string> _value2;
    private List<double> _value4;
    private static XmlSerializer serializer;

    public Product()
    {
        this._value4 = new List<double>();
        this._value2 = new List<string>();
        this._body = new List<object>();
        this._header = new List<object>();
    }


    [System.Xml.Serialization.XmlElementAttribute("Header", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public List<object> Header
    {
        get
        {
            return this._header;
        }
        set
        {
            this._header = value;
        }
    }


    [System.Xml.Serialization.XmlElementAttribute("Body", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public List<object> Body
    {
        get
        {
            return this._body;
        }
        set
        {
            this._body = value;
        }
    }


    [System.Xml.Serialization.XmlElementAttribute("value2", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public List<string> value2
    {
        get
        {
            return this._value2;
        }
        set
        {
            this._value2 = value;
        }
    }


    [System.Xml.Serialization.XmlElementAttribute("value4", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public List<double> value4
    {
        get
        {
            return this._value4;
        }
        set
        {
            this._value4 = value;
        }
    }

Serialisers and Deserialisers etc.

}
}

#pragma warning restore

Of course, I can validate it by myself, but my company wants to generate the restrictions from XSD to be faster and to avoid errors. This was the reason for purcasing the professional version of xsd2code.

Maybe someone has more experiences than me, thank you in advance!

The support finally answered my question and solved my problem. Restrictions were not generated for enumerable properties. It was a bug and should be solved in the next version (4.2.0.54 and above).

Anyway, thank you for your time!

I can confirm that the latest version of xsd2code++ (xsd2code.com) generates correctly all restrictions (also for enumerable properties).

xsd2code ++的屏幕截图

Here the result :

    [System.Xml.Serialization.XmlElementAttribute("value2", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
    [System.ComponentModel.DataAnnotations.StringLengthAttribute(22, MinimumLength = 20)]
    public List<string> value2
    {
        get
        {
            return this._value2;
        }
        set
        {
            System.ComponentModel.DataAnnotations.ValidationContext validatorPropContext = new System.ComponentModel.DataAnnotations.ValidationContext(this, null, null);
            validatorPropContext.MemberName = "value2";
            Validator.ValidateProperty(value, validatorPropContext);
            this._value2 = value;
        }
    }


    [System.Xml.Serialization.XmlElementAttribute("value4", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
    [System.ComponentModel.DataAnnotations.RangeAttribute(5, 6)]
    public List<int> value4
    {
        get
        {
            return this._value4;
        }
        set
        {
            System.ComponentModel.DataAnnotations.ValidationContext validatorPropContext = new System.ComponentModel.DataAnnotations.ValidationContext(this, null, null);
            validatorPropContext.MemberName = "value4";
            Validator.ValidateProperty(value, validatorPropContext);
            this._value4 = value;
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM