my game class is this
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
}
}
and my class to get data from an xml file into a list is this.
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
}
}
I want to add
imageData = XmlReadFile.XmlDataToList(string nameOfXmlFile)
in the update field.
What I am trying to do is call the XmlFileRead method from the Game class while passing it the location of the xml file and then read the xml file and return a list back to the Main Game class.
My problem is the method is not even showing up in the Main Game class. Can someone please help to solve the problem. Please don't only point out the errors but help me with a solution as I am new to C# and Monogame.
答案 0 :(得分:0)
我用下面的代码解决了这个问题。
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);