title: Error using XMLReader in C# and XNA 4.0
body: <p>I'm trying to write a ContentTypeSerializer in XNA for Dictionaries of shared elements, and I'm almost there but have got an error when deserializing the dictionary xml, due to my lack of understanding of the XmlReader class.</p>
  
  <p>I use this function to serialize the dictionary (works fine):</p>
  
  <pre><code>protected override void Serialize(IntermediateWriter output,
                                        SharedResourceDictionary&lt;T, K&gt; value,
                                        ContentSerializerAttribute format)
      {
           foreach (KeyValuePair&lt;T, K&gt; item in value)// foreach (T item in value)
          {
              output.Xml.WriteStartElement(itemFormat.ElementName);
              output.WriteObject(item.Key, keyFormat);
              output.WriteSharedResource(item.Value, valueFormat);
              output.Xml.WriteEndElement();
          }
      }
  </code></pre>
  
  <p>And it generates this XML: <a href="http://pastebin.com/19fEteqV" rel="nofollow">http://pastebin.com/19fEteqV</a> (sorry, I don't manage to post it here with xml formatting)</p>
  
  <p>And finally I try to use this function to deserialize:</p>
  
  <pre><code>protected override SharedResourceDictionary&lt;T, K&gt; Deserialize(IntermediateReader input,
                                                           ContentSerializerAttribute format,
                                                           SharedResourceDictionary&lt;T, K&gt; existingInstance)
      {
          if (existingInstance == null)
              existingInstance = new SharedResourceDictionary&lt;T, K&gt;();
  
          while (input.MoveToElement(itemFormat.ElementName))
          {
  
              T key;
  
              input.Xml.ReadToDescendant(keyFormat.ElementName);
              key = input.ReadObject&lt;T&gt;(keyFormat);
              input.Xml.ReadToNextSibling(valueFormat.ElementName);
              input.ReadSharedResource(
                  valueFormat, 
                  (K value) =&gt; existingInstance.Add(key, value));
              input.Xml.MoveToElement();
  
  
          }
  
          return existingInstance;
      }
  </code></pre>
  
  <p>The problem is that when I attempt to load I get the following exception: </p>
  
  <pre><code>Microsoft.Xna.Framework.Content.Pipeline.InvalidContentException was unhandled
    Message=XML element "Resources" not found.
    Source=Microsoft.Xna.Framework.Content.Pipeline
    StackTrace:
         at Microsoft.Xna.Framework.Content.Pipeline.Serialization.Intermediate.IntermediateReader.ReadSharedResources()
         at Microsoft.Xna.Framework.Content.Pipeline.Serialization.Intermediate.IntermediateSerializer.Deserialize[T](XmlReader input, String referenceRelocationPath)
         at SerializationTest.Modes.Mode4.Update(GameTime gameTime) in D:\All\Proyects\SerializationTest\SerializationTest\SerializationTest\Modes\Mode4.cs:line 87
         at Microsoft.Xna.Framework.Game.Update(GameTime gameTime)
         at SerializationTest.Game1.Update(GameTime gameTime) in D:\All\Proyects\SerializationTest\SerializationTest\SerializationTest\Game1.cs:line 78
         at Microsoft.Xna.Framework.Game.Tick()
         at Microsoft.Xna.Framework.Game.HostIdle(Object sender, EventArgs e)
         at Microsoft.Xna.Framework.GameHost.OnIdle()
         at Microsoft.Xna.Framework.WindowsGameHost.RunOneFrame()
         at Microsoft.Xna.Framework.WindowsGameHost.ApplicationIdle(Object sender, EventArgs e)
         at System.Windows.Forms.Application.ThreadContext.System.Windows.Forms.UnsafeNativeMethods.IMsoComponent.FDoIdle(Int32 grfidlef)
         at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
         at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
         at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
         at System.Windows.Forms.Application.Run(Form mainForm)
         at Microsoft.Xna.Framework.WindowsGameHost.Run()
         at Microsoft.Xna.Framework.Game.RunGame(Boolean useBlockingRun)
         at Microsoft.Xna.Framework.Game.Run()
         at SerializationTest.Program.Main(String[] args) in D:\All\Proyects\SerializationTest\SerializationTest\SerializationTest\Program.cs:line 15
    InnerException: 
  </code></pre>
  
  <p>The complete code can be found <a href="http://pastebin.com/mrgfC1Fm" rel="nofollow">here</a>. Didn't want to cluster the post with all of it.
  If anyone has any suggestion, it is much appreciated. I'm pretty sure the error is on parsing the xml in the deserialize function but I can't find it for the life of me.</p>
  
  <p>Thank you for your time.</p>