简体   繁体   English

类名以父节点的类名为前缀:使用xsd.exe从XML生成CS代码

[英]Class name being prefixed with class name of parent node: CS code generation from XML using xsd.exe

I am trying to generate a C# file from a XML file using xsd.exe. 我正在尝试使用xsd.exe从XML文件生成C#文件。 I am facing a problem that every class is prefixed with the class name of its parent node. 我面临的问题是每个类都以其父节点的类名为前缀。 So it is generating very long names, depending on the depth of an XML element. 因此,它会生成很长的名称,具体取决于XML元素的深度。 I am posting a sample. 我发布了一个样本。
Sample.xml sample.xml中

<?xml version="1.0" encoding="UTF-8"?>
<A attrib1="100">
    <B attrib2="200">
        <C attrib3="300" />
    </B>
</A>    

On submitting xsd Sample.xml command I get Sample.xsd as below: 在提交xsd Sample.xml命令时,我得到Sample.xsd ,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
  <xs:element name="A">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="B" minOccurs="0" maxOccurs="unbounded">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="C" minOccurs="0" maxOccurs="unbounded">
                <xs:complexType>
                  <xs:attribute name="attrib3" type="xs:string" />
                </xs:complexType>
              </xs:element>
            </xs:sequence>
            <xs:attribute name="attrib2" type="xs:string" />
          </xs:complexType>
        </xs:element>
      </xs:sequence>
      <xs:attribute name="attrib1" type="xs:string" />
    </xs:complexType>
  </xs:element>
  <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
    <xs:complexType>
      <xs:choice minOccurs="0" maxOccurs="unbounded">
        <xs:element ref="A" />
      </xs:choice>
    </xs:complexType>
  </xs:element>
</xs:schema>

On submitting xsd sample.xsd /classes command I get Sample.cs as following: 在提交xsd sample.xsd /classes命令时,我得到Sample.cs如下:

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.269
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

using System.Xml.Serialization;

// 
// This source code was auto-generated by xsd, Version=4.0.30319.1.
// 


/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
public partial class A {

    private AB[] bField;

    private string attrib1Field;

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

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

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class AB {

    private ABC[] cField;

    private string attrib2Field;

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

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

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class ABC {

    private string attrib3Field;

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

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
public partial class NewDataSet {

    private A[] itemsField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("A")]
    public A[] Items {
        get {
            return this.itemsField;
        }
        set {
            this.itemsField = value;
        }
    }
}

The problem is it is producing classes AB and ABC , which are being used in class A and class AB respectively. 问题是它正在产生AB类和ABC类,它们分别在A类和AB类中使用。 This is creating very long names in real scenario. 这在真实场景中创建了很长的名称。 Is there a way to suppress this behavior, so that I get classes as A, B and C , instead of A, AB and ABC ? 有没有办法抑制这种行为,所以我得到的类是A,B和C ,而不是A,AB和ABC Please use my example as reference. 请用我的例子作为参考。

You need to give names and definitions to all your complex types explicitly in the schema and then reference these types when defining the elements. 您需要在模式中明确地为所有复杂类型提供名称和定义,然后在定义元素时引用这些类型。 The XSD.exe tool will work with these "type" names as class names. XSD.exe工具将使用这些“类型”名称作为类名。 An example is shown for A. You need to break out all complex types similarly in the schema. 为A显示了一个示例。您需要在模式中类似地分解所有复杂类型。

<xs:element name="A" type="AType"/>

<xs:complexType name="AType">
    <xs:sequence>
        <xs:element name="B" type="BType" minOccurs="0" maxOccurs="unbounded"/>
    </xs:sequence>
    <xs:attribute name="attrib1" type="xs:string"/>
</xs:complexType>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM