使用 XSD.exe 时如何指定属性名称?
我正在编写一个 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 运行它时,我最终会得到 FileStructure
、Folder
的类,和文件
。 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能 - XSD 会为您做到这一点。您无法影响 C# 代码最终的外观。
您可以尝试做的是使用 XSD 中的类型,而不是内联定义所有内容。例如,定义一个
,然后在您的主定义中使用它:也许这会给您带来更多的命名灵活性,因为它将类型及其定义解耦来自用作元素名称的实际名称。
另外,一般来说,当我将事物定义为显式类型(复杂或简单类型),然后只使用这些定义的类型,而不是使用大量复杂的内联定义时,我发现我的 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: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.