4

I have an XSD describing the schema for some of my data contracts in an application I am building. I'm not the most experienced in writing xsd files, but so far nothing I've tried has been able to resolve my issue.

I have a section of the schema that looks like this:

<xs:complexType name="RecordSetDefinition">
  <xs:sequence>
    <xs:element name="RecordId" type="RecordIdDefinition" minOccurs="0" maxOccurs="unbounded" />
    <xs:element name="Record" type="RecordDefinition" minOccurs="0" maxOccurs="unbounded" />
  </xs:sequence>
  <xs:attribute name="Id" type="xs:string" />
</xs:complexType>

The XML that this corresponds to looks like:

<RecordSet Id="Test">
  <RecordId ...>
    ...
  </RecordId>
  <RecordId ...>
    ...
  </RecordId>
  <Record ...>
    ...
  </Record>
  <Record ...>
    ...
  </Record>
  <Record ...>
    ...
  </Record>
</RecordSet>

The idea is that my data contract for the RecordSetDefinition has an array of RecordIdDefinitions and RecordDefinitions. Running the generate command:

xsd \c \l:cs

Produces:

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.1432")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="...")]
public partial class RecordSetDefinition {

    private RecordIdDefinition[] recordIdField;

    private RecordDefinition[] recordField;

    private string idField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("RecordId")]
    public RecordIdDefinition[] RecordId {
        get {
            return this.recordIdField;
        }
        set {
            this.recordIdField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("Record")]
    public RecordDefinition[] Record {
        get {
            return this.recordField;
        }
        set {
            this.recordField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string Id {
        get {
            return this.idField;
        }
        set {
            this.idField = value;
        }
    }
}

Which is all fine and dandy...except that the names of the properties containing the arrays of definitions are singular, not plural.

Question:

To aid in code comprehension, I'd like to have these names pluralized (i.e. RecordIds instead of RecordId). Is this possible to do using the XSD so that xsd.exe will generate plural array names in C# while still maintaining the individual element names singular in XML?

The XML is arbitrary and can be changed, but so far I can't seem to find a permutation of the schema that gives me my desired outcome. The closest I've gotten is making an element called RecordIds which contains the individual RecordId records, but that still results in a rather unintuitive RecordIds.RecordId[] property. Using the xs:choice would generate RecordIds.Items[] which would work, but I'd like to know if there is a better way.

I can't just modify the generated code since obviously it would be overwritten the next time we generate the .cs files from the .xsd and that poses maintainability issues for the next developer who comes along.

Los Frijoles
  • 4,771
  • 5
  • 30
  • 49

1 Answers1

0

I'm not sure this is possible with xsd.exe

I think the easiest solution would be finding better names. How about RecordIdList.RecordId[]?

Another approach could be to use partial classes. If you could find a way to get your RecordIds in a partial class you could regenerate the rest of the code without changing them.

Also you could try to use xsd2code. It doesn't have the ability to automatically generate plural names, but as it is open source you could change it to do that for you.

Another solution could be to transform the code after generating it. Write a script that modifies the .cs file after it has been generated.

My choices would be.

  1. find a better name
  2. leave it as is and possibly add a comment to the code on why the name is singular so that the next developer knows what is going on.