使用 XSD.exe 时如何指定属性名称?

发布于 2024-08-12 07:14:47 字数 1108 浏览 1 评论 0原文

我正在编写一个 XSD 架构,其中有一个描述文件结构的元素:

<xs:schema
  ...
>
  <xs:element name="FileStructure">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="Folder" minOccurs="1" maxOccurs="1" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="Folder">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="Folder" minOccurs="0" maxOccurs="unbounded" />
        <xs:element ref="File" minOccurs="0" maxOccurs="unbounded" />
      </xs:sequence>
      <xs:attribute name="name" type="xs:string" use="required" />
    </xs:complexType>
  </xs:element>
  <xs:element name="AccessionFile">
    <xs:complexType mixed="true">
      ...
    </xs:complexType>
  </xs:element>
</xs:schema>

当我通过 XSD.exe 运行它时,我最终会得到 FileStructureFolder 的类,和文件。 FileStructure有一个名为Folder的属性,它保存一个文件夹数组;文件夹有一个名为Folder1的属性,它保存文件夹数组。

我不希望文件夹上的属性被称为Folder1。使用 XSD.exe 时如何指定属性和类型的名称?

I'm writing an XSD schema which has an element that describes a file structure:

<xs:schema
  ...
>
  <xs:element name="FileStructure">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="Folder" minOccurs="1" maxOccurs="1" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="Folder">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="Folder" minOccurs="0" maxOccurs="unbounded" />
        <xs:element ref="File" minOccurs="0" maxOccurs="unbounded" />
      </xs:sequence>
      <xs:attribute name="name" type="xs:string" use="required" />
    </xs:complexType>
  </xs:element>
  <xs:element name="AccessionFile">
    <xs:complexType mixed="true">
      ...
    </xs:complexType>
  </xs:element>
</xs:schema>

When I run this through XSD.exe, I end up with classes for FileStructure, Folder, and File. FileStructure has a property called Folder which holds an array of Folders; Folder has a property called Folder1 which holds an array of Folders.

I don't want the property on Folder to be called Folder1. How do I specify names for properties and types when using XSD.exe?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

离去的眼神 2024-08-19 07:14:47

您不能 - XSD 会为您做到这一点。您无法影响 C# 代码最终的外观。

您可以尝试做的是使用 XSD 中的类型,而不是内联定义所有内容。例如,定义一个 ,然后在您的主定义中使用它:

  <xs:complexType name="FolderType">
    <xs:sequence>
      <xs:element name="Folder" type="FolderType" minOccurs="0" maxOccurs="unbounded" />
      <xs:element ref="File" minOccurs="0" maxOccurs="unbounded" />
    </xs:sequence>
    <xs:attribute name="name" type="xs:string" use="required" />
  </xs:complexType>

  <xs:sequence>
     <xs:element name="Folder" type="FolderType" minOccurs="1" maxOccurs="1" />
  </xs:sequence>

也许这会给您带来更多的命名灵活性,因为它将类型及其定义解耦来自用作元素名称的实际名称。

另外,一般来说,当我将事物定义为显式类型(复杂或简单类型),然后只使用这些定义的类型,而不是使用大量复杂的内联定义时,我发现我的 XML 模式更容易阅读、理解和清晰。元素及其内联类型。

You can't - XSD will do that for you. You cannot influence how the C# code will end up looking.

What you could try to do is use types in XSD, instead of defining everything inline. E.g. define a <xs:complexType name="FolderType"> and then use this in your main definition:

  <xs:complexType name="FolderType">
    <xs:sequence>
      <xs:element name="Folder" type="FolderType" minOccurs="0" maxOccurs="unbounded" />
      <xs:element ref="File" minOccurs="0" maxOccurs="unbounded" />
    </xs:sequence>
    <xs:attribute name="name" type="xs:string" use="required" />
  </xs:complexType>

  <xs:sequence>
     <xs:element name="Folder" type="FolderType" minOccurs="1" maxOccurs="1" />
  </xs:sequence>

Maybe that gives you a bit more flexibility for naming, since it decouples the type and its definition from the actual name being used as element name.

Also, in general, I find my XML schema to be easier to read and understand and cleaner when I define things as explicit types (complex or simple types) and then just use those defined types, instead of having large amounts of convoluted inline definitions of elements and their inline types.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文