Sign in to follow this  
FictionJVR

C# reading XML file problem

Recommended Posts

So I basicly need a way to read a tile editor's format.
It's the first time that I actually have to use xml files, and I'm tired and I am just completely stuck on this problem.
Maybe it's something silly and I am just overlooking it constantly.

And yes don't worry, this is just a quick test of something I found and that I just quickly threw into a new project and added some stuff for testing.
it's basicly just to understand how it works, not an actual project.

C# code:
[SPOILER]
[code]

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
using System.IO.Compression;

namespace XMLparseTest
{
class Program
{
static void Main(string[] args)
{

using (var stream = System.IO.File.OpenText("CaveTest.tmx"))
using (var reader = XmlReader.Create(stream))
while (reader.Read())
{
var name = reader.Name;

switch (reader.NodeType)
{
case XmlNodeType.DocumentType:
if (name != "map")
throw new Exception("Invalid map format");
break;
case XmlNodeType.Element:
switch (name)
{
case "map":
{
Console.WriteLine("Version:" + reader.GetAttribute("version"));
Console.WriteLine("Orientation:" + reader.GetAttribute("orientation"));
Console.WriteLine("Width:" + reader.GetAttribute("width"));
Console.WriteLine("Height:" + reader.GetAttribute("height"));
Console.WriteLine("tilewidth:" + reader.GetAttribute("tilewidth"));
Console.WriteLine("tileheight:" + reader.GetAttribute("tileheight"));

} break;

case "tileset":
{
using (var st = reader.ReadSubtree())
{
st.Read();
Tile(st);
}
} break;

case "layer":
{
using (var st = reader.ReadSubtree())
{
st.Read();
Layer(st);
}
} break;
}
break;
case XmlNodeType.EndElement:
break;
case XmlNodeType.Whitespace:
break;
}
}
}

static void Layer(XmlReader reader)
{
Console.WriteLine("LAYER__________________");
Console.WriteLine("Name:" + reader.GetAttribute("name"));
Console.WriteLine("Width:" + reader.GetAttribute("width"));
Console.WriteLine("Height:" + reader.GetAttribute("height"));

int Width = 10;
int Height = 10;
int Tiles = Width * Height;

while (!reader.EOF)
{
var name = reader.Name;
switch (reader.NodeType)
{
case XmlNodeType.Element:
if (name == "data")
{
var encoding = reader.GetAttribute("encoding");
var compressor = reader.GetAttribute("compression");
switch (encoding)
{
case "base64":
{
int dataSize = (Width * Height * 4) + 1024;
var buffer = new byte[dataSize];
reader.ReadElementContentAsBase64(buffer, 0, dataSize);

Stream stream = new MemoryStream(buffer, false);
if (compressor == "gzip")
stream = new GZipStream(stream, CompressionMode.Decompress, false);

using (stream)
using (var br = new BinaryReader(stream))
{
for (int i = 0; i < Tiles; i++)
Console.WriteLine(":" + br.ReadInt32());
}

continue;
};

default:
throw new Exception("Unrecognized encoding.");
}
}

break;
case XmlNodeType.EndElement:
break;
}
}
}

static void Tile(XmlReader reader)
{
Console.WriteLine("TILE_____________________________________________");
Console.WriteLine("Name:" + reader.GetAttribute("name"));
Console.WriteLine("Firstgid:" + reader.GetAttribute("firstgid"));
Console.WriteLine("Tilewidth:" + reader.GetAttribute("tilewidth"));
Console.WriteLine("Tileheight:" + reader.GetAttribute("tileheight"));

while (reader.Read())
{
var name = reader.Name;

switch (reader.NodeType)
{
case XmlNodeType.Element:
switch (name)
{
case "image":
Console.WriteLine("Source:" + reader.GetAttribute("source"));
Console.WriteLine("width:" + reader.GetAttribute("width"));
Console.WriteLine("height:" + reader.GetAttribute("height"));
break;

}

break;
case XmlNodeType.EndElement:
break;
}
}
}
}
}
[/code]
[/SPOILER]

XML file:
[SPOILER]
[code]


<?xml version="1.0" encoding="UTF-8"?>
<map version="1.0" orientation="isometric" width="10" height="10" tilewidth="64" tileheight="32">
<tileset firstgid="1" name="tiled_cave_1" tilewidth="64" tileheight="128">
<image source="tiled_cave_1.png" width="1024" height="1920"/>
</tileset>
<layer name="Tilelaag 2" width="10" height="10">
<data encoding="base64" compression="gzip">
H4sIAAAAAAAAC22PyQrAIBBD7aqVInSBQq/S5f+/sBEykJYeHhNn4mgq51wNGtCCFQTSA086cFIH+nrq0ss8DySzFt8ocy87bnpsnxePvVXmkfUCO1h+sD1WZ9Ej9QGmD5u8o5kD/5Akl2WwbHonffqnzCxDZC+6d65ESr4H2qPxD5ABAAA=
</data>
</layer>
<layer name="Tilelaag 1" width="10" height="10">
<data encoding="base64" compression="gzip">
H4sIAAAAAAAAC7NkYGAwAuIwIDYD4iAobQRlg2grIA5kIAyMgdgQi3g7FnUw81qhdDEJ5sFACRI7lgj3EVKH7E5izUN2XxORevABAC5lMZ2QAQAA
</data>
</layer>
</map>

[/code]
[/SPOILER]



Basicly I get everything from <map > and <tileset>, but then it just stops at "TileLaag2": name/width/heigth.

Debugging tells me that this:
var name = reader.Name;
////

if (name == "data")
{

that name = layer, but it should be data.


So the problem is that I can't figure out why it isn't switching to data


EDIT

Fixed already, took a break to eat and came back and noticed I missed " reader.Read();" in the while loop. <<

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this