title: C# WPF XML Schema validation of abstract type
body: <p>First of all I would like to say hallo everybody - it's my first post here on SO. Now important stuff. I am writing some code in C# WPF .NET 4.0 which is based on serialization / deserialization built-in mechanizms. Everything was fine untill I decided to create a validation schema for my serialized xml file. All of this would be more clear when I show a bit my code:</p>
  
  <p><strong>Serialization</strong></p>
  
  <pre><code>private void serializeObjectsToFile(string path)
  {
      using (var writer = new System.IO.StreamWriter(path))
      {
          var serializer = new XmlSerializer(myTopClass.GetType());
          serializer.Serialize(writer, myTopClass);
          writer.Flush();
      }
  }   
  </code></pre>
  
  <p><strong>Deserialization</strong></p>
  
  <pre><code>private MyTopClass deserializeObjectsFromFile(string path)
  {
      using (var stream = System.IO.File.OpenRead(path))
      {
          var serializer = new XmlSerializer(typeof(MyTopClass));
          return serializer.Deserialize(stream) as MyTopClass;
      }
  }
  </code></pre>
  
  <p><strong>Validation</strong></p>
  
  <pre><code>private bool validateXmlAgainstSchema(string pathToFile)
  {
      XmlSchemaSet schemas = new XmlSchemaSet();
      schemas.Add("", schemaPath);
  
      XDocument inputFile = XDocument.Load(pathToFile);
      var isFileValid = true;
      inputFile.Validate(schemas, (o, e) =&gt;
          {
              MessageBox.Show(string.Format("Validation fails. {0}.", e.Message));
              isFileValid = false;
          });
      return isFileValid;
  }        
  </code></pre>
  
  <p>Now let me introduce my Model which is serialized to xml file and for which I want to create a validation schema.</p>
  
  <p><strong>Top Class</strong></p>
  
  <pre><code>namespace SerializationVsSchemaTest.Model
  {
      [Serializable]
      public class MyTopClass
      {
          public int myTopItemOne { get; set; }
          public int myTopItemTwo { get; set; }
          public int myTopItemThree { get; set; }
          public MyAbstractClass myTopAbstractItem { get; set; }
  
          public MyTopClass()
          {
              myTopItemOne = 7;
              myTopItemTwo = 8;
              myTopItemThree = 9;
              myTopAbstractItem = new ClassOne();
          }
  
          public override string ToString()
          {
              return string.Format("{0}\n{1}\n{2}\n{3}\n", myTopItemOne, myTopItemTwo, myTopItemThree, myTopAbstractItem.ToString());
          }
      }
  }
  </code></pre>
  
  <p><strong>Abstract Class - the most important stuff here and the root part of my question</strong></p>
  
  <pre><code>namespace SerializationVsSchemaTest.Model
  {
      [XmlInclude(typeof(ClassOne))]
      [XmlInclude(typeof(ClassTwo))]
      public abstract class MyAbstractClass
      {
      }
  }
  </code></pre>
  
  <p><strong>ClassOne</strong></p>
  
  <pre><code>namespace SerializationVsSchemaTest.Model
  {
      [Serializable]
      public class ClassOne : MyAbstractClass
      {
          public int argOne;
          public int argTwo;
          public int argThree;
  
          public ClassOne()
          {
              argOne = -1;
              argTwo = -2;
              argThree = -3;
          }
      }
  }
  </code></pre>
  
  <p><strong>ClassTwo</strong></p>
  
  <pre><code>namespace SerializationVsSchemaTest.Model
  {
      [Serializable]
      public class ClassTwo: MyAbstractClass
      {
          public byte argOne;
          public byte argTwo;
          public byte argThree;
  
          public ClassTwo()
          {
              argOne = 4;
              argTwo = 5;
              argThree = 6;
          }
      }
  }
  </code></pre>
  
  <p><strong>Serialization result</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;MyTopClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
      &lt;myTopItemOne&gt;7&lt;/myTopItemOne&gt;
      &lt;myTopItemTwo&gt;8&lt;/myTopItemTwo&gt;
      &lt;myTopItemThree&gt;9&lt;/myTopItemThree&gt;
      &lt;myTopAbstractItem xsi:type="ClassOne"&gt;
          &lt;argOne&gt;-1&lt;/argOne&gt;
          &lt;argTwo&gt;-2&lt;/argTwo&gt;
          &lt;argThree&gt;-3&lt;/argThree&gt;
      &lt;/myTopAbstractItem&gt;
  &lt;/MyTopClass&gt;
  </code></pre>
  
  <p><strong>My validation schema</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;xs:schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xs="http://www.w3.org/2001/XMLSchema" attributeFormDefault="unqualified" elementFormDefault="qualified"&gt;
  &lt;xsd:element name="MyTopClass"&gt;
      &lt;xsd:complexType&gt;
          &lt;xsd:sequence&gt;
              &lt;xsd:element name="myTopItemOne" type="xsd:unsignedByte" /&gt;
              &lt;xsd:element name="myTopItemTwo" type="xsd:unsignedByte" /&gt;
              &lt;xsd:element name="myTopItemThree" type="xsd:unsignedByte" /&gt;
              &lt;xsd:element name="myTopAbstractItem"&gt;
                  &lt;xsd:complexType&gt;
                      &lt;xsd:sequence&gt;
                          &lt;xsd:element name="argOne" type="xsd:byte" /&gt;
                          &lt;xsd:element name="argTwo" type="xsd:byte" /&gt;
                          &lt;xsd:element name="argThree" type="xsd:byte" /&gt;
                      &lt;/xsd:sequence&gt;
                  &lt;/xsd:complexType&gt;
              &lt;/xsd:element&gt;
          &lt;/xsd:sequence&gt;
      &lt;/xsd:complexType&gt;
  &lt;/xsd:element&gt;
  </code></pre>
  
  <p></p>
  
  <p>And the problem here is that <strong>this schema fails to validate my xml</strong> with following result:
  <em>Validation fails. Invalid type xsi:type 'ClassOne'.</em> The strangest thing is that this schema is generated by Visual Studio 2013 using my serialized xml file. So it should match 100%... I hope You guys will help me somehow. Cheers!</p>