簡體   English   中英

在C#和XNA 4.0中使用XMLReader時出錯

[英]Error using XMLReader in C# and XNA 4.0

我正在嘗試在XNA中為共享元素的字典編寫ContentTypeSerializer,但我快到了,但是由於不了解XmlReader類,在反序列化字典xml時遇到了錯誤。

我使用此函數序列化字典(工作正常):

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,但是我一生都找不到它。

感謝您的時間。

您沒有閱讀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;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM