mganss / XmlSchemaClassGenerator

Generate C# classes from XML Schema files
Apache License 2.0
590 stars 179 forks source link

[Required] Attribute #87

Open vytcus opened 5 years ago

vytcus commented 5 years ago

Hi, I've started using this tool instead of xsd.exe and so far it has been a godsend. However, I notice that the "minOccurs", "maxOccurs" restrictions are not reflected on the generated classes (no [required] attribute or similar). Is this part of the functionallity?

Thank you.

mganss commented 5 years ago

The DataAnnotation attributes are only emitted for XML schema simple types with restrictions, e.g.

<xs:simpleType name="Password">
    <xs:restriction base="xs:string">
        <xs:minLength value="5"/>
        <xs:maxLength value="40"/>
    </xs:restriction>
</xs:simpleType>

results in

[System.ComponentModel.DataAnnotations.MinLengthAttribute(5)]
[System.ComponentModel.DataAnnotations.MaxLengthAttribute(40)]
public string Password { get; set; }
vytcus commented 5 years ago

Cheers for the answer. That clarified things, hope you don't mind answering another one. Some other annotations are not reflected in the class too. Here's the schema:

<xsd:complexType name='FooType'>
        <xsd:annotation>
            <xsd:documentation xml:lang="en">
                <ccts:UniqueID>123</ccts:UniqueID>
                <ccts:ObjectClassQualifierTerm>ENUM</ccts:ObjectClassQualifierTerm>
                <ccts:OfficialConstraintContextValue>
                    <CodeListEntry><value>01</value><definition>Male</definition></CodeListEntry>
                    <CodeListEntry><value>02</value><definition>Female</definition></CodeListEntry>
                </ccts:OfficialConstraintContextValue>
            </xsd:documentation>
        </xsd:annotation>
        <xsd:sequence>
            <xsd:element name='value' minOccurs='1' maxOccurs='1'>
                <xsd:simpleType>
                    <xsd:restriction base='xsd:string'>
                        <xsd:pattern value='(01|02){1}'/>
                    </xsd:restriction>
                </xsd:simpleType>
            </xsd:element>
        </xsd:sequence>
    </xsd:complexType>
}

I get

/// <summary>
    /// <para xml:lang="en">123</para>
    /// </summary>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("XmlSchemaClassGenerator", "2.0.174.0")]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    public partial class FooType
    {

        /// <summary>
        /// <para xml:lang="en">Pattern: (01|02){1}.</para>
        /// </summary>
        [System.ComponentModel.DataAnnotations.RegularExpressionAttribute("(01|02){1}")]
        [System.Xml.Serialization.XmlElementAttribute("value", Form=System.Xml.Schema.XmlSchemaForm.Unqualified, DataType="string")]
        public string Value { get; set; }
    }

Basically, what's inside <ccts:ObjectClassQualifierTerm> and <ccts:OfficialConstraintContextValue> elements did not get represented in the generated class. Any reason why?

Again, thank you for you answers.

mganss commented 5 years ago

We're only getting the text of the first element from within the xs:documentation element. Most documentation contains only raw text and no markup. Using the markup would lead to illegal XML comments and compiler warnings. We could use the concatenated text from all child nodes.