tags:

views:

27

answers:

1

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?

+2  A: 

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.

marc_s