3

I am trying to generate a C# file from a XML file using xsd.exe. 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. I am posting a sample.
Sample.xml

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

On submitting xsd Sample.xmlcommand I get Sample.xsd as below:

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

//------------------------------------------------------------------------------
// <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. 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? Please use my example as reference.

sabertooth1990
  • 1,048
  • 1
  • 13
  • 19

1 Answers1

2

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. An example is shown for A. You need to break out all complex types similarly in the schema.

<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>
Noggin Head
  • 161
  • 4