开发者

Error using XMLReader in C# and XNA 4.0

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.

I use this function to serialize the dictionary (works fine):

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();
        }
    }

And it generates this XML: http://pastebin.com/19fEteqV (sorry, I don't manage to post it here with xml formatting)

And finally I try to use this function to 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(
                valueFormat, 
                (K value) => existingInstance.Add(key, value));
            input.Xml.MoveToElement();


        }

        return existingInstance;
    }

The problem is that when I attempt to load I get the following exception:

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.ComponentManag开发者_如何学Goer.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: 

The complete code can be found here. 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.

Thank you for your time.


You weren't reading the ending element of your Item tag, so the reader was going wild after reading the first key/value pair. Here is the corrected Deserialize function:

    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;
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜