0

I'm programatically generating an XSD file from some C# classes using XmlReflectionImporter. My goal is to generate a schema representing XML similar to the following:

<MyCriteriaList MinimumMatches="2">
  <Criterion>First Criterion</Criterion>
  <Criterion>Second ...</Criterion>
  ...
</MyCriteriaList>

So, an example XSD representing this would be:

<xs:element name="MyCriteriaList" type="ArrayOfCriterion" />
...
<xs:complexType name="ArrayOfCriterium">
  <xs:sequence>
    <xs:element name="Criterion" type="xs:string" />
  </xs:sequence>
  <xs:attribute name="MinimumMatches" type="xs:integer" />
</xs:complexType>

The class that would represent the array of criterion:

public class Criteria : IList<Criterion>
{
    [XmlAttribute]
    public int MinimumMatches { get; set; }

    // IList implementation...
}

However, this generates the following XSD:

<xs:complexType name="ArrayOfCriterion">
  <xs:sequence>
    <xs:element name="Criterion" type="xs:string" />
  </xs:sequence>
</xs:complexType>

The property MinimumMatches is ignored completely and not added as an attribute to ArrayOfCriterion.

It's as if the XmlReflectionImporter (or possibly the XmlSchemaExporter which is using the result of the importer) is detecting Criteria is an implementation of IList and deciding it's a simple array, completely discarding any properties that have been added over and above the interface implementation.

Any tips greatly appreciated.

Regards,

Steve.

Stevo
  • 1,424
  • 11
  • 20
  • `XmlSerializer` itself doesn't support collection properties. See [How/why does XmlSerializer treat a class differently when it implements IList](http://stackoverflow.com/questions/31552724/how-why-does-xmlserializer-treat-a-class-differently-when-it-implements-ilistt) or [Ist there a way to serialize public properties of a class that implements ICollection](http://stackoverflow.com/questions/16810133/ist-there-a-way-to-serialize-public-properties-of-a-class-that-implements-icolle) – dbc Aug 06 '15 at 08:47
  • Thanks, that second link was useful. I'll follow the advice "don't do that"! – Stevo Aug 06 '15 at 09:38

0 Answers0