读取XML文件(C#)

时间:2017-03-04 06:03:34

标签: c# xml

我目前正在使用GDI +在C#中开发一个游戏引擎,并且已经达到了我想将地图加载到我的引擎中的程度。我正在使用Tiled Map Editor(http://www.mapeditor.org/),它允许我非常轻松地创建地图并将它们保存到XML文件中。我从来没有真正用XML做任何事情,真的需要一些帮助来阅读它。

以下是XML文件的内容:( pastebin因为它太大了),“数据/图层”行是在第80行附近

http://pastebin.com/z906fdbW

这是我到目前为止阅读它的代码。出于某种原因,我的瓷砖数似乎是零,我不知道为什么。任何帮助都会很棒。

    public static void LoadTMX(Screen screen, SpriteSheet sprs, string path)
    {
        try
        {
            using (FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read))
            {
                List<int> gids = new List<int>();

                XmlDocument xdoc = new XmlDocument();
                xdoc.Load(stream);

                int width = int.Parse(xdoc.DocumentElement.GetAttribute("width"));
                int height = int.Parse(xdoc.DocumentElement.GetAttribute("height"));
                int tileWidth = int.Parse(xdoc.DocumentElement.GetAttribute("tilewidth"));
                int tileHeight = int.Parse(xdoc.DocumentElement.GetAttribute("tileheight"));

                XmlNodeList layers = xdoc.DocumentElement.SelectNodes("layer");

                for (int i = 0; i < layers.Count; i++)
                {
                    XmlNodeList tiles = layers[i].SelectSingleNode("data").SelectNodes("tile");

                    // why is the tile count zero?
                    Console.WriteLine(tiles.Count);
                }
            }
        }
        catch(Exception e)
        {
            throw e;
        }
    }     int tileWidth = int.Parse(xdoc.DocumentElement.GetAttribute("tilewidth"));
                int tileHeight = int.Parse(xdoc.DocumentElement.GetAttribute("tileheight"));

                XmlNodeList layers = xdoc.DocumentElement.SelectNodes("layer");

                for (int i = 0; i < layers.Count; i++)
                {
                    XmlNodeList tiles = layers[i].SelectSingleNode("data").SelectNodes("tile");


                    Console.WriteLine(tiles.Count);
                }
            }
        }
        catch(Exception e)
        {
            throw e;
        }
    }

1 个答案:

答案 0 :(得分:0)

我已经测试了你的代码,但似乎工作正常;它返回1024两次

output

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
    {
var path = Path.Combine(Path.GetDirectoryName(Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory())), "XMLFile1.xml");
        LoadTMX(path);
 }
    public static void LoadTMX( string path)
    {
        try
        {
            using (FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read))
            {
                List<int> gids = new List<int>();
XmlDocument xdoc = new XmlDocument();
                xdoc.Load(stream);
int width = int.Parse(xdoc.DocumentElement.GetAttribute("width"));
int height = int.Parse(xdoc.DocumentElement.GetAttribute("height"));
int tileWidth = int.Parse(xdoc.DocumentElement.GetAttribute("tilewidth"));
int tileHeight = int.Parse(xdoc.DocumentElement.GetAttribute("tileheight"));
XmlNodeList layers = xdoc.DocumentElement.SelectNodes("layer");
for (int i = 0; i < layers.Count; i++)
                {
                    XmlNodeList tiles = layers[i].SelectSingleNode("data").SelectNodes("tile");
Console.WriteLine(tiles.Count);
                }
            }
            Console.ReadKey();
        }
        catch (Exception e)
        {
            throw e;
        }

    }
}
}