title: Nested DataContract serialization exception WCF
body: <p>I have the following DataContracts which want to serialize to:</p>
  
  <pre><code>[DataContract]
  public class SwepubHeader
  {
      [DataMember(Name = "xsearch")]
      public SwepubBody Body { get; set; }
  }
  
  [KnownType(typeof(SwepubSearchItem))]
  [DataContract]
  public class SwepubBody
  {
      [DataMember(Name = "from")]
      public int From { get; set; }
  
      [DataMember(Name = "to")]
      public int To { get; set; }
  
      [DataMember(Name = "records")]
      public int Records { get; set; }
  
      [DataMember(Name = "list")]
      public SwepubSearchItem[] SearchItems { get; set; }
  }
  [DataContract]
  public class SwepubSearchItem
  {
      [DataMember(Name = "isbn")]
      public object ISBN { get; set; }
  
      [DataMember(Name = "title")]
      public object Title { get; set; }
  
      [DataMember(Name = "description")]
      public object Description { get; set; }
  
      [DataMember(Name = "identifier")]
      public object Identifier { get; set; }
  
      [DataMember(Name = "type")]
      public object Type { get; set; }
  
      [DataMember(Name = "publisher")]
      public object Publisher { get; set; }
  
      [DataMember(Name = "date")]
      public object Date { get; set; }
  
      [DataMember(Name = "language")]
      public object Language { get; set; }
  
      [DataMember(Name = "relation")]
      public object Relation { get; set; }
  
      [DataMember(Name = "subject")]
      public object Subjects { get; set; }
  
      [DataMember(Name = "creator")]
      public object Creators { get; set; }
  }
  </code></pre>
  
  <p>When when i try to return this via WCF i get the following exception:</p>
  
  <blockquote>
    <p>There was an error while trying to serialize parameter <a href="http://tempuri.org/:DoSwepubSearchAdvancedResult" rel="nofollow">http://tempuri.org/:DoSwepubSearchAdvancedResult</a>. The InnerException message was 'Type 'System.Object[]' with data contract name 'ArrayOfanyType:<a href="http://schemas.microsoft.com/2003/10/Serialization/Arrays" rel="nofollow">http://schemas.microsoft.com/2003/10/Serialization/Arrays</a>' is not expected. Consider using a DataContractResolver or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.'.  Please see InnerException for more details.</p>
  </blockquote>
  
  <p>I understand i probobly need to do something with KnownTypeAttribute but i havn't really grasped how it is supposed to work. I tried adding SwepubSearchItem as a knowtype of Swepub body but that didn't work. Notworthy is that  if i would comment out the SearchItems property of the SwepubBody DataContract the code would work but I wouldnt get the most important part of the response :)</p>
  
  <p>Here is an example of the json that's being serialized:
  <a href="http://libris.kb.se/xsearch?query=barn+design+subject:(utveckling)&amp;format=json&amp;format_level=full&amp;database=swepub" rel="nofollow">http://libris.kb.se/xsearch?query=barn+design+subject:(utveckling)&amp;format=json&amp;format_level=full&amp;database=swepub</a></p>