在c#和XNA 4.0中使用XMLReader出错

本文关键字:XMLReader 出错 XNA | 更新日期: 2023-09-27 18:06:22

我试图在XNA中为共享元素的字典编写ContentTypeSerializer,并且我几乎在那里,但是在反序列化字典xml时出现错误,由于我缺乏对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: 

完整的代码可以在这里找到。我不想把所有的帖子都放在一起。如果有人有任何建议,我将不胜感激。我很确定错误是在反序列化函数中解析xml,但我找不到它。

感谢您的宝贵时间。

在c#和XNA 4.0中使用XMLReader出错

您没有读取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;
    }
}