C#如何在代码中编写xml文件,以便xna接受加载它?

时间:2017-05-26 12:19:14

标签: c# xml xna

在我认为成功编写xml文档之后,xna拒绝加载它说" XML不是XNA中间格式。缺少XnaContent根元素。"。 类似问题的几个答案建议更改属性,但最后我希望在运行时创建此文件并在运行时读取。 除了十几个stackoverflow答案之外,我还阅读了大部分关于XmlDocument的msdn条目 - 没有找到一个神奇的" PutDocInIntermediateXNAFormat"方法,多么令人惊讶(我当然没有找到一个字面上这个名字的方法)。 我确定我只是错过了所有Youtube视频中关于创建一个我认为已知的xml文件的最简单的部分。 所以在我浪费任何时间之前,有人可以抓住我的手并指导我吗?

编辑: 它接触xna并不满意我尝试使用与底层类不对应的xml文件。无论如何我都可以加载它?

EDIT2:
我创建了一个类,并使用此类创建了xml文件。我没有问题将它保存为xml文件,也没有将其加载回类instanze。 然而我确实在使用这些xml文件作为xna的资产方面遇到了问题 这是班级:

namespace TheLemmingsGuideToMagrathea
{
 public class LevelSave
 {
     public int lemmingsToSpawn;
     public int lemmingsToSave;

     public int remainingStairs;
     public int remainingQuests;
     public int remainingGuidesv2;
     public int remainingThumps;
     public int remainingConciousnesses;

     public string[] map;
     public Point start;
     public Point finish;

     public int timeInSeconds;
 }
}

以下是从该类生成xmlFiles的代码:

LevelSave save = new LevelSave();
save.map = level.Stringify();
save.start  = startLocation;
save.finish = finishLocation;
save.lemmingsToSpawn   = 50;
save.lemmingsToSave    = 25;
save.timeInSeconds     = 5 * 60;
save.remainingStairs   = 10;
save.remainingQuests   = 10;
save.remainingGuidesv2 = 10;
save.remainingThumps   = 10;
save.remainingConciousnesses = 10;

XmlSerializer serializer = new XmlSerializer(typeof(LevelSave));

if (!Directory.Exists(Path.Combine("Content", "Levels")))
    Directory.CreateDirectory(Path.Combine("Content", "Levels"));
if (!Directory.Exists(Path.Combine("Content", "Levels", levelNameTextBox.Text)))
    Directory.CreateDirectory(Path.Combine("Content", "Levels", levelNameTextBox.Text));
StreamWriter writer = new StreamWriter(Path.Combine("Content", "Levels", levelNameTextBox.Text, levelNameTextBox.Text + ".xml"));
serializer.Serialize(writer, save);
writer.Close();

这是由该代码生成的一个xmlFile:

<?xml version="1.0" encoding="utf-8"?>
<LevelSave xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <lemmingsToSpawn>50</lemmingsToSpawn>
  <lemmingsToSave>25</lemmingsToSave>
  <remainingStairs>10</remainingStairs>
  <remainingQuests>10</remainingQuests>
  <remainingGuidesv2>10</remainingGuidesv2>
  <remainingThumps>10</remainingThumps>
  <remainingConciousnesses>10</remainingConciousnesses>
  <map>
    <string> 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0</string>
    <string> 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0</string>
    <string> 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0</string>
    <string> 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0</string>
    <string> 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0</string>
    <string> 0,  0, 80,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0</string>
    <string> 0,  0, 80, 80,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0</string>
    <string> 0,  0,  0, 80, 80,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0</string>
    <string> 0,  0,  0,  0, 80, 80,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0</string>
    <string> 0,  0,  0,  0,  0, 80, 80,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0</string>
    <string>11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11</string>
    <string>10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10</string>
    <string>10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10</string>
    <string>10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10</string>
    <string>10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10</string>
    <string>10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10</string>
    <string>10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10</string>
    <string>10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10</string>
    <string>10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10</string>
    <string>10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10</string>
  </map>
  <start>
    <X>1</X>
    <Y>9</Y>
  </start>
  <finish>
    <X>19</X>
    <Y>2</Y>
  </finish>
  <timeInSeconds>300</timeInSeconds>
</LevelSave>

我尝试添加了 <XnaContent> <Asset Type ="LevelSave">
手动标记,确实修复了它,最后让我编译 - 除了它没有。它现在给出错误There was an error while deserializing intermediate XML. Cannot find type "LevelSave".(我不知道不能再用作内容了。)
我在精神上做了,请帮助我。

1 个答案:

答案 0 :(得分:0)

从“缺少XnaContent根元素”消息中,我假设您的XML文档的根目录中没有名为XnaContent的元素...

https://blogs.msdn.microsoft.com/shawnhar/2008/08/12/everything-you-ever-wanted-to-know-about-intermediateserializer/

您生成的XML看起来像这样......

<?xml version="1.0" encoding="utf-8"?>
<XnaContent>
  <Asset Type="TestClass">
    <GetSetProperty>Hello World</GetSetProperty>
    <PublicField>1</PublicField>
    <Nested>
      <Name>Shawn</Name>
      <IsEnglish>true</IsEnglish>
    </Nested>
  </Asset>
</XnaContent>

记下<XnaContent>根元素......