Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we control type name in xsd.exe tool generated class

xsd.exe tool generates classes for given xsd file. It follows a pattern for naming the type. For example,

<Students>
  <Student Name="A" RollNo="1" Address="Some Address">
    <Department Id="20" Name="CSE"/>
  </Student> 
</Students>

xsd.exe /c Students.xsd

Students.cs file is generated. If we see the type for elements

Element     Type Name
Students    Students
Student     StudentsStudent
Department  StudentsStudentDepartment

It generates type by prefixing the parent element name if the element is child. Can we control this name? I need the type name as same as element name. Student => Student, Department => Department

Thanks in Advance

like image 600
Manikandan Avatar asked Feb 13 '13 12:02

Manikandan


People also ask

What is XSD exe?

The XML Schema Definition (Xsd.exe) tool generates XML schema or common language runtime classes from XDR, XML, and XSD files, or from classes in a runtime assembly.

How do I edit an XSD file?

To edit a fileSelect the Use XML editor to view and edit the underlying XML Schema file link on the Start View. The XML editor appears with the new file open. Copy the XML Schema sample code from Purchase order schema and paste it to replace the code that was added to the new XSD file by default.

How do I give attributes in XSD?

The " use" property in the XSD definition is used to specify if the attribute is optional or mandatory. To specify that an attribute must be present, use = "required" (Note use may also be set to "prohibited", but we'll come to that later).

What is XSD data type?

The <xs:decimal> data type is used to represent numeric values. It supports decimal numbers up to 18 digits.


1 Answers

In general, one cannot customize the names of the generated classes when using xsd.exe - unlike mechanisms available elsewhere, for e.g. a JAXB user through custom JAXB binding file.

However, for xsd.exe the generated class names depend on the authoring style. What you described, is generated when the authoring style is conformant to the "Russian Doll" i.e. as below:

<?xml version="1.0" encoding="utf-8"?>
<!--XML Schema generated by QTAssistant/XML Schema Refactoring (XSR) Module (http://www.paschidev.com)-->
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:element name="Students">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="Student">
          <xsd:complexType>
            <xsd:sequence>
              <xsd:element name="Department">
                <xsd:complexType>
                  <xsd:attribute name="Id" type="xsd:unsignedByte" use="required" />
                  <xsd:attribute name="Name" type="xsd:string" use="required" />
                </xsd:complexType>
              </xsd:element>
            </xsd:sequence>
            <xsd:attribute name="Name" type="xsd:string" use="required" />
            <xsd:attribute name="RollNo" type="xsd:unsignedByte" use="required" />
            <xsd:attribute name="Address" type="xsd:string" use="required" />
          </xsd:complexType>
        </xsd:element>
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>
</xsd:schema>

If you change its style to something different (this is called a Venetian Blind):

<?xml version="1.0" encoding="utf-8"?>
<!--XML Schema generated by QTAssistant/XML Schema Refactoring (XSR) Module (http://www.paschidev.com)-->
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="Students" type="Students"/>
    <xsd:complexType name="Students">
        <xsd:sequence>
            <xsd:element name="Student" type="Student"/>
        </xsd:sequence>
    </xsd:complexType>
    <xsd:complexType name="Student">
        <xsd:sequence>
            <xsd:element name="Department" type="Department"/>
        </xsd:sequence>
        <xsd:attribute name="Name" type="xsd:string" use="required"/>
        <xsd:attribute name="RollNo" type="xsd:unsignedByte" use="required"/>
        <xsd:attribute name="Address" type="xsd:string" use="required"/>
    </xsd:complexType>
    <xsd:complexType name="Department">
        <xsd:attribute name="Id" type="xsd:unsignedByte" use="required"/>
        <xsd:attribute name="Name" type="xsd:string" use="required"/>
    </xsd:complexType>
</xsd:schema>

You'll be getting these classes generated:

enter image description here

like image 120
Petru Gardea Avatar answered Oct 12 '22 13:10

Petru Gardea