2011-08-23 20 views
0

我正在嘗試在XNA中爲共享元素的字典編寫ContentTypeSerializer,而且我幾乎在那裏,但是由於缺乏理解,反序列化字典xml時出現錯誤的XmlReader類。錯誤在C#和XNA 4.0中使用XMLReader

我用這個功能來序列化字典(正常工作):

protected override void Serialize(IntermediateWriter output, 
             SharedResourceDictionary<T, K> value, 
             ContentSerializerAttribute format) 
    { 
     foreach (KeyValuePair<T, K> 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(); 
     } 
    } 

而且它生成此XML:http://pastebin.com/19fEteqV(對不起,我不管理與XML格式張貼在這裏)

最後我嘗試使用此功能反序列化:

protected override SharedResourceDictionary<T, K> Deserialize(IntermediateReader input, 
                 ContentSerializerAttribute format, 
                 SharedResourceDictionary<T, K> existingInstance) 
    { 
     if (existingInstance == null) 
      existingInstance = new SharedResourceDictionary<T, K>(); 

     while (input.MoveToElement(itemFormat.ElementName)) 
     { 

      T key; 

      input.Xml.ReadToDescendant(keyFormat.ElementName); 
      key = input.ReadObject<T>(keyFormat); 
      input.Xml.ReadToNextSibling(valueFormat.ElementName); 
      input.ReadSharedResource(
       valueFormat, 
       (K value) => existingInstance.Add(key, value)); 
      input.Xml.MoveToElement(); 


     } 

     return existingInstance; 
    } 

的問題是,當我嘗試加載我得到以下異常:

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: 

完整的代碼可以在here找到。不想將所有文章聚集在一起。 如果有人有任何建議,非常感謝。我很確定這個錯誤是在解析反序列化函數中的xml,但是我無法在我的生活中找到它。

謝謝你的時間。

回答

0

您沒有閱讀您的Item標籤的結尾元素,因此讀者在閱讀第一個鍵/值對後狂野不已。這是修正的Deserialize功能:

protected override SharedResourceDictionary<T, K> Deserialize(IntermediateReader input, 
                  ContentSerializerAttribute format, 
                  SharedResourceDictionary<T, K> existingInstance) 
    { 
     if (existingInstance == null) 
      existingInstance = new SharedResourceDictionary<T, K>(); 

     while (input.MoveToElement(Itemformat.ElementName)) 
     { 
      T key; 

      input.Xml.ReadToDescendant(Keyformat.ElementName); 
      key = input.ReadObject<T>(Keyformat); 
      input.Xml.ReadToNextSibling(Valueformat.ElementName); 
      input.ReadSharedResource<K>(Valueformat, (K value) => 
      { 
       existingInstance.Add(key, value); 
      }); 
      input.Xml.ReadEndElement(); 
     } 

     return existingInstance; 
    } 
}