3

I have an xml file that will contain a number of articles details.

So far I've created the XML and XSD - here's the XSD:

  <xs:simpleType name="ArticleKey">
    <xs:restriction base="xs:string">
      <xs:enumeration value="Key1" />
      <xs:enumeration value="Key2" />
    </xs:restriction>
  </xs:simpleType>
  <xs:element name="Articles">
    <xs:complexType>
      <xs:sequence>
        <xs:element maxOccurs="unbounded" name="Article">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="Key" type="ArticleKey" />
              <xs:element name="Title" type="xs:string" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

I've also manually created the class object:

public class Article
{
    public ArticleKey Key { get; set; }

    public string Title { get; set; }
}

In order to do this I've also had to create the enum 'ArticleKey' separately like this:

public enum ArticleKey
{
    Key1,
    Key2
}

However, this obviously leads to the maintenance of duplicate enum values in the XSD and as a standalone enum.

Is there a way to link the two and avoid the maintenance issue or is this as easy as it gets?

dotnetnoob
  • 10,783
  • 20
  • 57
  • 103

1 Answers1

1

If you're maintaining them separately, then I think that's as good as you'll get.

However as an alternative approach, could you generate all your xsd from your c# class, thereby removing all duplication of effort.

You can use xsd.exe to do this (from your VS installation). This SO question covers it more fully.

Community
  • 1
  • 1
Jon Egerton
  • 40,401
  • 11
  • 97
  • 129
  • Thanks for that Jon, however its probably just as clunky (from a maintenance point of view) as the solution I have at the moment. – dotnetnoob Oct 17 '13 at 11:15
  • Really? - you'd just add the xsd.exe command to a post build event and have them generated out for you? – Jon Egerton Oct 17 '13 at 11:16