我的游戏课就是这个
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace testReadXmlGame
{
/// <summary>
/// This is the main type for your game.
/// </summary>
public class Game1 : Game
{
GraphicsDeviceManager graphics;
static List<List<string>> imageData = new List<List<string>>();
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
// TODO: Add your drawing code here
base.Draw(gameTime);
}
#region Public methods
#endregion
}
}
我的类将xml文件中的数据导入到列表中。
using System;
using System.Collections.Generic;
using System.Xml;
namespace testReadXmlGame
{
public class XmlFileRead
{
#region Fields
private bool addToFile = false;
List<List<string>> imageData = new List<List<string>>();
#endregion
#region Public methods
public List<List<string>> XmlDataToList(string pathToXmlFile)
{
XmlReader xmlReader = XmlReader.Create(pathToXmlFile);
while (xmlReader.Read())
{
List<string> tempData = new List<string>(); // create a temp list to add data from each node
while (xmlReader.MoveToNextAttribute())
{
tempData.Add(xmlReader.Value);
addToFile = true;
}
if (addToFile)
{
imageData.Add(tempData);
addToFile = false;
}
}
// below code used to check the contents of the list returned
for (int i = 1; i <= imageData.Count - 1; i++)
{
for (int j = 0; j < imageData[i].Count; j++)
{
Console.WriteLine(imageData[i][j]);
}
}
Console.ReadKey();
return imageData;
}
#endregion
}
}
我想补充一下
imageData = XmlReadFile.XmlDataToList(string nameOfXmlFile)
在更新字段中。
我想要做的是从Game类中调用XmlFileRead方法,同时传递xml文件的位置,然后读取xml文件并将列表返回给Main Game类。
我的问题是该方法甚至没有出现在主游戏类中。有人可以帮助解决问题。请不要只指出错误,而是帮助我解决方案,因为我是C#和Monogame的新手。
我用下面的代码解决了这个问题。
namespace testReadXmlGame
{
public static class XmlFileRead
{
#region public methods
public static List<List<string>> XmlDataToList(string pathToXmlFile, bool addToFile, List<List<string>> imageData)
{
XmlReader xmlReader = XmlReader.Create(pathToXmlFile);
while (xmlReader.Read())
{
List<string> tempData = new List<string>(); // create a temp list to add data from each node
while (xmlReader.MoveToNextAttribute())
{
tempData.Add(xmlReader.Value);
addToFile = true;
}
if (addToFile)
{
imageData.Add(tempData);
addToFile = false;
}
}
return imageData;
}
#endregion
}
}
在主游戏类中声明以下内容
static List<List<string>> imageData = new List<List<string>>();
bool addToFile = false;
然后从主类调用XmlFileRead.XmlDataToList
imageData = XmlFileRead.XmlDataToList("alienExplode.xml", addToFile, imageData);