使用工作注释将XSD转换为C#的免费工具/扩展

时间:2012-08-29 09:17:46

标签: c# xsd xsd.exe xsd2code

是否有任何工具或版本的XSD2Code或xsd.exe可以与XSD2Code的注释一起生成C#实体?

XSD2Code和xsd.exe都忽略了注释(对于XSD2Code,EnableSummaryComment只是运行不正常)我不想花时间分析和更改其中任何一个的源代码...有没有人知道是否有是完全可行的免费替代方案吗?

3 个答案:

答案 0 :(得分:2)

XmlSchemaClassGenerator支持元素,属性和类型的注释。它还根据限制和它的开源生成XML文档。完全披露:我是主要作者。

/// <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; }
}

答案 1 :(得分:0)

你有一个你所追求的例子吗?例如,使用xsd2code这个

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

产生这个

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

你是在追求不同的东西吗?

答案 2 :(得分:0)

我刚用xsd2code和下面的xsd对此进行了测试。看起来xsd2code只支持在生成的C#代码中出现属性的注释。

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