简体   繁体   English

在C#和XNA 4.0中使用XMLReader时出错

[英]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. 我正在尝试在XNA中为共享元素的字典编写ContentTypeSerializer,但我快到了,但是由于不了解XmlReader类,在反序列化字典xml时遇到了错误。

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) 并生成此XML: http : //pastebin.com/19fEteqV (对不起,我无法以xml格式在此处发布)

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.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: 

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. 我敢肯定,错误在于解析反序列化功能中的xml,但是我一生都找不到它。

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. 您没有阅读Item标记的结尾元素,因此读者在阅读了第一个键/值对之后就疯狂了。 Here is the corrected Deserialize function: 这是更正后的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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM