title: DataContractSerializer not deserializing references
body: <p>I am using the DataContractSerializer in .NET 3.5 to deserialize xml.  The xml was previously serialized from a group of related entities in an entity model, backed by the entity framework 3.5.  There are many references, and the xml extensively contains all the values of the members and keys of each referenced entity.</p>
  
  <p>The top level entity deserializes fine, but the referenced entities do not.</p>
  
  <p>This is the code I'm using to serialize and deserialize:</p>
  
  <pre><code>    public static T DCDeserializeXml&lt;T&gt;(string xml)
      {
          MemoryStream memoryStream = new MemoryStream(Encoding.Unicode.GetBytes(xml));
          using (
          XmlDictionaryReader reader = XmlDictionaryReader.CreateTextReader(memoryStream, Encoding.Unicode,
                     new XmlDictionaryReaderQuotas(), null))
          {
              DataContractSerializer dataContractSerializer = new DataContractSerializer(typeof(T), null, Int32.MaxValue, false, true, null);
              return (T)dataContractSerializer.ReadObject(reader, true);
          }
      }
  
      public static string DCSerializeToXml&lt;T&gt;(T obj)
      {
          DataContractSerializer dataContractSerializer = new DataContractSerializer(typeof(T), null, Int32.MaxValue, false, true, null);
  
          String text;
          using (MemoryStream memoryStream = new MemoryStream())
          {
              dataContractSerializer.WriteObject(memoryStream, obj);                
              byte[] data = new byte[memoryStream.Length];
              Array.Copy(memoryStream.GetBuffer(), data, data.Length);
              text = Encoding.UTF8.GetString(data);
          }
          return text;
      }
  </code></pre>
  
  <p>This is a snippet of the XML:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;Assets&gt;
      &lt;Asset z:Id="i1" xmlns="http://schemas.datacontract.org/2004/07/XLayer" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/"&gt;
          &lt;EntityKey z:Id="i2" xmlns="http://schemas.datacontract.org/2004/07/System.Data.Objects.DataClasses" xmlns:a="http://schemas.datacontract.org/2004/07/System.Data"&gt;
              &lt;a:EntityContainerName&gt;XModelContainer&lt;/a:EntityContainerName&gt;
              &lt;a:EntityKeyValues&gt;
                  &lt;a:EntityKeyMember&gt;
                      &lt;a:Key&gt;AssetGUID&lt;/a:Key&gt;
                      &lt;a:Value i:type="z:guid"&gt;7424f615-43db-4834-b15a-5befa46bfd55&lt;/a:Value&gt;
                      &lt;/a:EntityKeyMember&gt;&lt;/a:EntityKeyValues&gt;
                      &lt;a:EntitySetName&gt;AssetSet&lt;/a:EntitySetName&gt;
                      &lt;/EntityKey&gt;
                      &lt;AssetGUID&gt;7424f615-43db-4834-b15a-5befa46bfd55&lt;/AssetGUID&gt;
                      &lt;Created&gt;2011-06-23T13:34:12.893&lt;/Created&gt;
                      &lt;Description/&gt;
                      &lt;npAudioInfoReference xmlns:a="http://schemas.datacontract.org/2004/07/System.Data.Objects.DataClasses"&gt;
                          &lt;a:EntityKey i:nil="true" xmlns:b="http://schemas.datacontract.org/2004/07/System.Data"/&gt;
                      &lt;/npAudioInfoReference&gt;
                      &lt;npCampaigns/&gt;
                      &lt;npCategory z:Id="i3"&gt;
                          &lt;EntityKey z:Id="i4" xmlns="http://schemas.datacontract.org/2004/07/System.Data.Objects.DataClasses" xmlns:a="http://schemas.datacontract.org/2004/07/System.Data"&gt;
                              &lt;a:EntityContainerName&gt;XModelContainer&lt;/a:EntityContainerName&gt;
                              &lt;a:EntityKeyValues&gt;
                                  &lt;a:EntityKeyMember&gt;
                                      &lt;a:Key&gt;CategoryID&lt;/a:Key&gt;
                                      &lt;a:Value i:type="b:int" xmlns:b="http://www.w3.org/2001/XMLSchema"&gt;1&lt;/a:Value&gt;
                                  &lt;/a:EntityKeyMember&gt;
                              &lt;/a:EntityKeyValues&gt;
                              &lt;a:EntitySetName&gt;AssetCategorySet&lt;/a:EntitySetName&gt;
                          &lt;/EntityKey&gt;
                          &lt;AM_DataDocumentTypes/&gt;
                          &lt;CategoryID&gt;1&lt;/CategoryID&gt;
                          &lt;CategoryName&gt;Generic Content&lt;/CategoryName&gt;
                          &lt;npAssets&gt;
  </code></pre>
  
  <p>I've been stuck on this for a couple days and I've exhausted all search results that I could find.  Using this technique can clearly avoid hand writing tons of code for each entity type in our model, of which there are 143.</p>
  
  <p>So to reiterate, the top level entity deserializes fine, but the referenced entities do not.  So Asset is loaded and Asset.AssetCategory (among many more) resolves to null after deserialization, and I need help to fix it so all references get instantiated.  Please, anyone?</p>