Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# XML Game
I am building a world editor for a game and I need help with the process of loading a map file. Everything is complete except for the map loading. I know how to load xml in c++ using tinyxml but my c# is really rusty. I just need to know how to take data from each of the elements so I can use them in the world editor. Any help would be massively appreciated.
 
this is an example of the file I am trying to load into my editor.
I know the layout of the xml is pretty poorly designed but I cannot modify it because I can not change the game client's LoadMap function.
 
<?xml version="1.0" encoding="UTF-8"?>
-<Map> -<tileset progPath="../../../../../../../../Desktop/TestTileset.png" 
	path="Desktop/TestTileset.png" tileSetSizeY="20" tileSetSizeX="20" tileHeigh="32" tileWidth="32" GridSizeH="5" GridSizeW="5">
		tilesetinfo
		<tileset index="0" y="0" x="0">tile</tileset>
		<tileset index="0" y="1" x="0">tile</tileset>
		<tileset index="0" y="2" x="0">tile</tileset>
		<tileset index="0" y="0" x="1">tile</tileset>
		<tileset index="0" y="1" x="1">tile</tileset>
		<tileset index="0" y="2" x="1">tile</tileset>
</tileset> 
 <item y="36" x="40" lane="2">waypoint</item>
 <item y="116" x="23" lane="2">waypoint</item>
 <item y="116" x="23" lane="2">node</item>
 <item y="120" x="72" lane="2">waypoint</item>
 <item y="120" x="72" lane="2">node</item>
 <item y="114" x="120" lane="2">waypoint</item>
 <item y="114" x="120" lane="2">node</item>
 <item y="70" x="138" lane="2">waypoint</item>
 <item y="116" x="23" lane="2" owner="1">tower</item>
 <item y="114" x="120" lane="2" owner="2">tower</item>
 <item y="30" x="117" owner="1">avatar</item>
 <item y="126" x="125" owner="2">avatar</item>
</Map>
Posted 15-Nov-12 3:50am
Carl Sr101

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

This is my short overview of what you can use:
 
  1. Use System.Xml.XmlDocument class. It implements DOM interface; this way is the easiest and good enough if the size if the document is not too big.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx[^].
  2. Use the class System.Xml.XmlTextReader; this is the fastest way of reading, especially is you need to skip some data.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx[^].
  3. Use the class System.Xml.Linq.XDocument; this is the most adequate way similar to that of XmlDocument, supporting LINQ to XML Programming.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx[^], http://msdn.microsoft.com/en-us/library/bb387063.aspx[^].
 
I don't clearly see from your question if you can define the file format by yourself or not. If you can, which seems likely, you don't have to work with XML directly at the level of the application. You should abstract out the data model from its persistence using serialization. Then I would advise to work only with the Data Contract:
http://msdn.microsoft.com/en-us/library/ms733127.aspx[^].
 
Please see also my past answers:
How can I utilize XML File streamwriter and reader in my form application?[^],
Creating property files...[^].
 
—SA
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web03 | 2.8.141015.1 | Last Updated 15 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100