XSD 名称生成

XSD name generation

我有一个 XSD 文件:

<xs:schema id="collections" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" targetNamespace="myNamespace" >
  <xs:element name="collections" msdata:IsDataSet="true" msdata:Locale="en-US">
    <xs:complexType>
      <xs:choice minOccurs="0" maxOccurs="unbounded">
        <xs:element name="collection">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="collectionDetails" minOccurs="0" maxOccurs="unbounded">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="transaction" minOccurs="0" maxOccurs="unbounded">
                    </xs:element>
                  </xs:sequence>
                  <xs:attribute name="Prop1" type="xs:string" />
                  <xs:attribute name="AccNo" type="xs:string" />
                </xs:complexType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:choice>
    </xs:complexType>
  </xs:element>
</xs:schema>

如果我用

解析这个 xsd
xsd.exe "my.xsd" /c  /n:"CollectionMessage" 

我得到以下输出

namespace CollectionMessage {
    using System.Xml.Serialization;

    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="myNamespace")]
    [System.Xml.Serialization.XmlRootAttribute(Namespace="myNamespace", IsNullable=false)]
    public partial class collections {

        private collectionsCollection[] itemsField;

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("collection", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public collectionsCollection[] Items {
            get {
                return this.itemsField;
            }
            set {
                this.itemsField = value;
            }
        }
    }

    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
    public partial class collectionsCollection {

        private collectionsCollectionCollectionDetails[] collectionDetailsField;
        [System.Xml.Serialization.XmlElementAttribute("collectionDetails", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public collectionsCollectionCollectionDetails[] collectionDetails {
            get {
                return this.collectionDetailsField;
            }
            set {
                this.collectionDetailsField = value;
            }
        }
    }

    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
    public partial class collectionsCollectionCollectionDetails {

        private object[] transactionField;

        private string prop1Field;

        private string accNoField;

        [System.Xml.Serialization.XmlElementAttribute("transaction", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public object[] transaction {
            get {
                return this.transactionField;
            }
            set {
                this.transactionField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string Prop1 {
            get {
                return this.prop1Field;
            }
            set {
                this.prop1Field = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string AccNo {
            get {
                return this.accNoField;
            }
            set {
                this.accNoField = value;
            }
        }
    }
}

但是,我想要一个保留名称的输出,并且不将父名称附加到子名称

例如

public partial class collectionsCollectionCollectionDetails {

应该输出为

public partial class collectionDetails { 

等等

我该如何完成?

恐怕,只要您不想更改 XSD,您就必须实现自己的代码生成逻辑才能获得预期的行为

您的问题可以使用 this post 中提供的解决方案来解决。您必须将编辑风格从本地(嵌套)更改为全局。这样,该工具将不再需要组成全局唯一的名称。