Miroslav
Miroslav

Reputation: 4703

Free tool / extension to convert XSD to C# with working annotations

Is there any tool or version of XSD2Code or xsd.exe that can generate C# entities together WITH comments from XSD2Code?

Both XSD2Code and xsd.exe ignore annotations (for XSD2Code, EnableSummaryComment just doesn't work well) and I don't want to spend time analyzing and changing the source code behind any of them...does anyone know if there is any fully working and free alternative?

Upvotes: 0

Views: 3128

Answers (3)

Michael Ganß
Michael Ganß

Reputation: 3406

XmlSchemaClassGenerator supports annotations on elements, attributes, and types. It also generates XML documentation from restrictions and it's open source. Full disclosure: I'm the main author.

/// <summary>
/// <para>Complex root type.</para>
/// <para>Information root.</para>
/// </summary>
[System.CodeDom.Compiler.GeneratedCodeAttribute("XmlSchemaClassGenerator", "1.0.0.0")]
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute("root", Namespace="http://example.org/annotations")]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlRootAttribute("root", Namespace="http://example.org/annotations")]
public partial class Root
{

    /// <summary>
    /// <para>
    ///            Test data in element.
    ///          </para>
    /// <para xml:lang="en">Minimum length: 1.</para>
    /// <para xml:lang="en">Maximum length: 50.</para>
    /// </summary>
    [System.ComponentModel.DataAnnotations.MinLengthAttribute(1)]
    [System.ComponentModel.DataAnnotations.MaxLengthAttribute(50)]
    [System.Xml.Serialization.XmlElementAttribute("TestElement", Namespace="http://example.org/annotations")]
    public string TestElement { get; set; }

    /// <summary>
    /// <para>
    ///          Optional test data in attribute.
    ///        </para>
    /// <para xml:lang="en">Minimum length: 1.</para>
    /// <para xml:lang="en">Maximum length: 50.</para>
    /// </summary>
    [System.ComponentModel.DataAnnotations.MinLengthAttribute(1)]
    [System.ComponentModel.DataAnnotations.MaxLengthAttribute(50)]
    [System.Xml.Serialization.XmlAttributeAttribute("TestAttribute", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string TestAttribute { get; set; }
}

Upvotes: 6

I've just tested this with xsd2code and the below xsd. It looks like xsd2code only supports annotations for attributes to appear in the generated C# code.

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:example="http://example.org/annotations"
           targetNamespace="http://example.org/annotations"
           elementFormDefault="qualified">
  <xs:annotation>
    <xs:documentation>
      Demonstration of xs:annotation xs:documentation blocks to appear in xsd2code generated C# code.
      Note: 
      - xsd.exe does not support annotations. Tested with NETFX 4.5.1 Tools.
      - xsd2code only for attributes. Teste with version 3.4.0.32990.
    </xs:documentation>
  </xs:annotation>
  <xs:element name="root" type="example:root">
    <xs:annotation>
      <xs:documentation>Information root.</xs:documentation>
    </xs:annotation>
  </xs:element>
  <xs:complexType name="root">
    <xs:annotation>
      <xs:documentation>Complex root type.</xs:documentation>
    </xs:annotation>
    <xs:all>
      <xs:annotation>
        <xs:documentation>
          Elements in any order.
        </xs:documentation>
      </xs:annotation>
      <xs:element name="TestElement" type="example:string50">
        <xs:annotation>
          <xs:documentation>
            Test data in element.
          </xs:documentation>
        </xs:annotation>
      </xs:element>
    </xs:all>
    <xs:attribute name="TestAttribute" use="optional" type="example:string50">
      <xs:annotation>
        <xs:documentation>
          Optional test data in attribute.
        </xs:documentation>
      </xs:annotation>
    </xs:attribute>
  </xs:complexType>
  <xs:simpleType name="string50">
    <xs:restriction base="xs:string">
      <xs:minLength value="1"/>
      <xs:maxLength value="50"/>
    </xs:restriction>
  </xs:simpleType>
  <xs:annotation>
    <xs:documentation>
      End of the XSD.
    </xs:documentation>
  </xs:annotation>
</xs:schema>

Upvotes: 0

Mightymuke
Mightymuke

Reputation: 5144

Do you have an example of what you are after? For example, with xsd2code this

<xs:attribute name="Test" use="optional">
    <xs:annotation>
        <xs:documentation>Test data number when combined with schema name provides the stub response.</xs:documentation>
    </xs:annotation>
    <xs:simpleType>
        <xs:restriction base="xs:string">
            <xs:minLength value="1"/>
            <xs:maxLength value="50"/>
        </xs:restriction>
    </xs:simpleType>
</xs:attribute>

produces this

/// <summary>
/// Test data number when combined with schema name provides the stub response.
/// </summary>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Test { get; set; }

Are you after something different?

Upvotes: 0

Related Questions