Skip to main content
GameDev.net gamedev.net
🔒 Locked

Working with XML

Started by hspirdal Aug 20, 2007 at 5:16 AM 7 replies 1.7k views
Original Post
hspirdal
hspirdal
I'm trying to seperate data stuff from the implementation part and doing so, I've added stuff like different races, classes, spells and such in each of their own XML file. I'm kinda new with XML and I'm having some trouble with reading from the file. Here's a xml example:

<?xml version="1.0" encoding="utf-8" ?>
<races>
	<race type="Dwarf" size="Medium" baseSpeed="20">
		<age minAge="40" middleAge="125" oldAge="188" venerableAge="250" maxAge="2d%" />
		<height maleBaseHeight="3.9" femaleBaseHeight="3.7" heightModifier="2d4" />
		<weight maleBaseWeight="130" femaleBaseWeight="100" weightModifier="2d6" />
		<favoredClass>Fighter</favoredClass>
		<abilityStatModifier str="0" dex="0" con="2" int="0" wis="0" cha="-2" />
		<knownLanguages>Common</knownLanguages>
		<knownLanguages>Dwarven</knownLanguages>
		<availableLanguages>Giant</availableLanguages>
		<availableLanguages>Gnome</availableLanguages>>
		<availableLanguages>Goblin</availableLanguages>
		<availableLanguages>Orc</availableLanguages>
		<availableLanguages>Terran</availableLanguages>
		<availableLanguages>Undercommon</availableLanguages>
		<hairColor>Black</hairColor>
		<hairColor>Brown</hairColor>
		<hairColor>Gray</hairColor>
		<hairColor>Charcoal</hairColor>
		<hairColor>Raven</hairColor>
		<eyeColor>Dark</eyeColor>
		<skinColor>Deep Tan</skinColor>
		<skinColor>Light Brown</skinColor>
	</race>
	<race type="Elf" size="Medium" baseSpeed="30">
		<age minAge="110" middleAge="175" oldAge="263" venerableAge="350" maxAge="4d%" />
		<height maleBaseHeight="4.5" femaleBaseHeight="4.5" heightModifier="2d6" />
		<weight maleBaseWeight="85" femaleBaseWeight="80" weightModifier="2d6" />
		<favoredClass>Wizard</favoredClass>
		<abilityStatModifier str="0" dex="2" con="-2" int="0" wis="0" cha="0" />
		<knownLanguages>Common</knownLanguages>
		<knownLanguages>Elven</knownLanguages>
		<availableLanguages>Gnome</availableLanguages>>
		<availableLanguages>Goblin</availableLanguages>
		<availableLanguages>Gnoll</availableLanguages>
		<availableLanguages>Orc</availableLanguages>
		<availableLanguages>Draconiac</availableLanguages>
		<availableLanguages>Sylvan</availableLanguages>
		<hairColor>Black</hairColor>
		<hairColor>Brown</hairColor>
		<hairColor>Chestnut</hairColor>
		<hairColor>Charcoal</hairColor>
		<hairColor>Raven</hairColor>
		<hairColor>Chocolate</hairColor>
		<hairColor>Cinnamon</hairColor>
		<hairColor>Ebony</hairColor>
		<hairColor>Mahogany</hairColor>
		<hairColor>Blonde</hairColor>
		<eyeColor>Green</eyeColor>
		<skinColor>Pale</skinColor>
	</race>
</races>

How could I search up the race which is of type Elf and get to its attributes and elements? How could i loop through all the elements? I've managed to get the value of the first item and its attribute values, but I feel like I'm stumbling in the dark. I've tried searching alot for stuff like "c# read xml attribute" and stuff like that, but the info on the subject is surprisingly sparse. It's probably too obvious. :)

System.Xml.XmlNode favoredClass = raceItems.Item(0).SelectSingleNode("race/favoredClass");
Console.Write("Favored class: {0}.\n", favoredClass.InnerText);

I would have thought that using raceItems.Item{1}... would give me the Elf element, but the value seems to have a different meaning.
TheTroll
TheTroll
Just a suggestion. Do not do your own reading to and from the files, use Serialization. Will save you a lot of problems in the long run.

theTroll
hspirdal
hspirdal
You're right, I'll do that. By the way, isn't there a way to compile in the data stored in xml files (and other datatypes) as some kind of resource or something as an alternative to serialization? I'm probably not wording myself correct here..
TheTroll
TheTroll
I for the most part just use binary serialization for my data files.

theTroll
jpetrie
jpetrie
I use the XPath stuff for the most part, or the serialization APIs -- although I tend to prefer the binary variants, using XML only for configuration stuff and in-development formats (most of which consequently are manipulated via XPath).
hspirdal
hspirdal
Hmm, I probably misunderstood earlier. You can fetch the XML data and then do serialization to it instead of re-reading the xml file all the time, right?
The only difference is that of loading the data from the storage type before you do the serialization of it.

I want to get more comfortable with XML as I can benefit from it in other languages as well. Having AD&D data and rules stored in XML, would save me some time later if I'd want to use it for another project, in another language.
hspirdal
hspirdal
Quote:
Original post by jpetrie
I use the XPath stuff for the most part, or the serialization APIs -- although I tend to prefer the binary variants, using XML only for configuration stuff and in-development formats (most of which consequently are manipulated via XPath).


Could you demonstrate the use of Xpath to solve my questions in the start of this thread?
TheTroll
TheTroll
Quote:
Original post by Nunyah
Hmm, I probably misunderstood earlier. You can fetch the XML data and then do serialization to it instead of re-reading the xml file all the time, right?
The only difference is that of loading the data from the storage type before you do the serialization of it.

I want to get more comfortable with XML as I can benefit from it in other languages as well. Having AD&D data and rules stored in XML, would save me some time later if I'd want to use it for another project, in another language.


Remember that they are all just streams. A file stream can be used like a memory stream. So you load the file into a memory stream if you need to reuse it a lot. Then you just use the memory stream for the de-serialization instead of the file stream.

theTroll
hspirdal
hspirdal
I've figured out my initial questions. In case there are other people wondering, you can get to an attribute value using ['attrName'] as an indexer of the current element.

This code will display all the race types stored in the Races.xml file:
System.Xml.XmlReader xr = System.Xml.XmlReader.Create(@"Races.xml");xr.ReadToDescendant("race");do{  Console.Write("Race: {0}.\n", xr["type"]);}while (xr.ReadToNextSibling("race"));                xr.Close();


@TheTroll:
I understand, kinda. I haven't yet been able to dig into streams completly, though, so there are still areas that can be shady and unclear.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.