0

In our project we are using Xsd2Code to generate c# code.

The XSD I am trying to adjust looks like this:

<?xml version="1.0"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="File">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Version" type="xs:string" />
        <xs:element name="Language" type="xs:string" />
        <xs:element name="Component" type="xs:anyType" minOccurs="1" maxOccurs="unbounded" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Within our project we now defined an abstract class Version. Is it possible to use our own class Version instead of string as the type of the element Version within the Xsd?

We would like to generate as many classes as possible using Xsd2Code. That's why I want to avoid to also having to create the File class by hand.

Thanks in advance for any help.

Thomas Huber
  • 1,282
  • 1
  • 13
  • 23

1 Answers1

0

You can create a new abstract class Version in xsd scheme and then make your defined Version class as partial and put it to the same namespace as the generated classes. By default all your generated classes must be partial.

Eugene Petrov
  • 651
  • 1
  • 5
  • 14
  • To generate the Version class using Xsd2Code is sadly not an option. That is why I am looking for a solution to keep at least the possibility to generate our File class. – Thomas Huber May 29 '12 at 10:38
  • Why not an option? You need to generate empty partial class. All logic you can put to your defined class. Generated class and defined class will be the same entity – Eugene Petrov May 29 '12 at 10:40
  • Ok I clearly did not understand your answer before. Sounds perfect. I will give it a try. Thanks in advance. – Thomas Huber May 29 '12 at 11:08
  • Ok I've managed to create a Version.xsd which is imported into my File.xsd. But xsd2code always creates an partial class Version within the File.Generated.cs which will lead to an ambiguous reference error since the Version class is in a different namespace. Even if I manage to get this approach to work, will XML Serialization still work since Version is an abstract class and the actual object will always be some derived type. – Thomas Huber May 29 '12 at 12:09
  • Don't use different namespace for Version class. You can use include instead import for it in xsd. If you want to support serialization for Version derived classes you must create a relevant class in xsd for each Version derived class and connect them throught partial feature. You can define all fields for serialization in a xsd class, but functions in a nornal defined one. – Eugene Petrov May 29 '12 at 12:19