11

xsd.exe 工具为给定的 xsd 文件生成类。它遵循命名类型的模式。例如,

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

xsd.exe /c Students.xsd

生成Students.cs 文件。如果我们看到元素的类型

Element     Type Name
Students    Students
Student     StudentsStudent
Department  StudentsStudentDepartment

如果元素是子元素,它会通过在父元素名称前加上前缀来生成类型。我们可以控制这个名字吗?我需要类型名称与元素名称相同。学生 => 学生,部门 => 部门

提前致谢

4

1 回答 1

22

通常,使用 xsd.exe 时无法自定义生成的类的名称 - 与其他地方可用的机制不同,例如 JAXB 用户通过自定义 JAXB 绑定文件。

但是,对于 xsd.exe,生成的类名取决于创作风格。您所描述的内容是在创作风格符合“俄罗斯娃娃”时生成的,即如下:

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

如果您将其样式更改为不同的样式(这称为威尼斯式百叶窗):

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

您将生成这些类:

在此处输入图像描述

于 2013-02-13T16:19:02.327 回答