简体   繁体   English

将数据从XML文件加载到XNA 4.0

[英]Loading data from XML file into XNA 4.0

This is a problem that I've been working on for a few days now. 这是我已经研究了几天的问题。 I managed to make a Game library that has 2 classes - one contains the name's and type's of variables I want to change, and the other is used to write into a XML file (serialization). 我设法制作了一个具有2个类的游戏库-一个包含要更改的变量的名称和类型,另一个用于写入XML文件(序列化)。 This is my serialization class: 这是我的序列化类:

namespace Postavke
{
    public class TempMain
    {
        public static void Main()
        {
            object TestValue = new PostavkeStanice();
            XmlWriterSettings XMLPostavke = new XmlWriterSettings();
            XMLPostavke.Indent = true;

            using (XmlWriter pisac = XmlWriter.Create("Postavke.xml", XMLPostavke))
            {
                IntermediateSerializer.Serialize(pisac, TestValue, null);
            }
        }
    }
}

The class that I want to serialize ("PostavkeStanica") has only one bool variable, so the XML file is very, very simple. 我要序列化的类(“ PostavkeStanica”)只有一个bool变量,因此XML文件非常非常简单。 The problem appears when I want to load the data from XML into my XNA 4.0 program. 当我要将数据从XML加载到XNA 4.0程序中时出现问题。 I use the ContentManager , but when I try to use the variables that should have their values set by this, I get the error 我使用ContentManager ,但是当我尝试使用应该由其设置其值的变量时,出现错误

"There was an error while deserializing intermediate XML. Cannot find type Postavke.PostavkeStanice"."

In declaration region: 在声明区域:

ContentManager upraviteljSadrzaja;
PostavkeStanice radnaStanica1;

In the LoadContent method: 在LoadContent方法中:

radnaStanica1 = upraviteljSadrzaja.Load<PostavkeStanice>(@"Teksture/Postavke");

The "PostavkeStanica" class has only one bool variable called "Stanje" which I want to use to determine if a certain object is enabled or disabled, for instance: “ PostavkeStanica”类只有一个bool变量“ Stanje”,我想使用它来确定某个对象是启用还是禁用,例如:

if (position == stanica1 && radnaStanica1.Stanje)
            {

                kontrola = false;
            }

This piece of code determines if a movable object is in front of radnaStancia1 object (which doesn't change its position) and when the contition is true it stops the movement. 这段代码确定可移动对象是否位于radnaStancia1对象的前面(不会改变其位置),并且当条件为true时,它将停止移动。 I'm trying to use the XML file to set the value of this variable because I want to be able to change the value of that variable without rebuilding the whole solution. 我试图使用XML文件来设置此变量的值,因为我希望能够在不重建整个解决方案的情况下更改该变量的值。

Please advise, thank you. 请指教,谢谢。

Sometimes when the deserializer can't find the type, it is either because the game library isn't referenced - or - during build time when the compiler is trying to make sense of that content.load statement, the game library that holds the type it is trying to find hasn't been built yet. 有时,当反序列化程序找不到类型时,可能是因为未引用游戏库-或-在编译期间编译器试图理解该content.load语句时,持有该类型的游戏库它试图找到尚未建立​​。

Make sure to set the dependency of the main game project to depend on the library AND reference it too. 确保将主游戏项目的依赖项设置为依赖于库并也引用它。 When the game library is set as a dependency, the compiler knows to build it first. 将游戏库设置为依赖项时,编译器知道首先要构建它。

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

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