Xml& C#反序列化异常错误

时间:2013-11-22 23:34:16

标签: c# xml-serialization visual-studio-2013

我正在开发XNA游戏项目并继续收到以下错误:

  

System.Xml.dll中出现未处理的“System.InvalidOperationException”类型异常

     

其他信息:XML文档中存在错误(2,2)。

这是我的Xml代码:

<?xml version="1.0" encoding="utf-8" ?>
<SplashScreen>
<Path>\SplashScreen\Game.png</Path>
</SplashScreen>

这是我的C#代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;

namespace WindowsGame3
{
    public class Manager<T>
    {
        public Type Type;

        public T Load(string Path)
        {
            T instance;
            using (TextReader reader = new StreamReader(Path))
            {
                XmlSerializer xml = new XmlSerializer(Type);
                instance = (T)xml.Deserialize(reader);
            }
            return instance;
        }

        public void save(string Path, object obj)
        {
            using (TextWriter writer = new StreamWriter(Path))
            {
                XmlSerializer xml = new XmlSerializer(Type);
                xml.Serialize(writer, obj);
            }
        }
    }
}

以下是使用上述Manager类的代码:

public void LoadContent(ContentManager Content)
{
    this.Content = new ContentManager(Content.ServiceProvider, "Content");

    GameScreenManager = new Manager<GameScreen>();
    GameScreenManager.Type = currentScreen.Type;

    currentScreen = GameScreenManager.Load("SplashScreen.xml"); 
    currentScreen.LoadContent();
}

1 个答案:

答案 0 :(得分:0)

您是否检查过调用T方法的类型Load是否匹配根元素名称SplashScreen的正确大小?