0

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

XSD2Code 和 xsd.exe 都忽略注释(对于 XSD2Code,EnableSummaryComment 只是不能很好地工作),我不想花时间分析和更改它们背后的源代码......有谁知道是否有任何完全工作和免费的选择?

4

3 回答 3

5

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; }
}
于 2016-09-30T11:08:39.300 回答
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; }

你追求不同的东西吗?

于 2012-11-08T22:32:44.117 回答
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>
于 2015-07-09T10:19:49.527 回答