Lesson 14.1: Moving game data to external files

In this lesson, we’ll start moving the game data to XML files, and change the factories to read from them.

After we do this, if you want to add more locations, monsters, items, quests, and recipes, you will only need to update these data files. You won’t need to modify the source code and rebuild the solution.

There will be three lessons to convert the ItemFactory to use XML data files, because I want to cover three new topics. This lesson will cover reading the XML file and using it to create objects. The next two will cover “generics” and “extension methods”.

Then, there will be a final lesson to convert all the other factories to read data from XML files.




Lesson Steps

Step 1: Create a new GameData folder in Engine

This is where we’ll add the game data files.


Step 2: Create Engine\GameData\GameItems.xml

To create this file, right-click on the GameData folder and select Add -> New Item… -> Visual C# Items -> Data -> XML File.


Select GameItems.xml in the Solution Explorer and view its properties. Set the value of “Copy to Output Directory” to “Copy Always”.


Now, when you build the program, Visual Studio will create a GameData folder in the same folder as the executable files and copy the GameItems.xml data file into the it.


XML (Extensible Markup Language) is a format for storing (or passing) data.

We’re already using XAML (a specialized form of XML) to define controls in the UI. Our data files will follow a similar style: opening and closing tags, attributes, and the possibility of an element having child elements.

In the Items.xml file (shown below), the first line is the “XML declaration” line. It tells what version of XML is being used, and what character set is being used. I’m using “utf-8” for the encoding. It’s the most-common encoding.


The “GameItems” node/element is the root element – all the other elements in this file are its children, because they are in between the “<GameItems>” opening tag and the “</GameItems>” closing tag.

GameItem’s three child elements are “Weapons”, “HealingItems”, and “MiscellaneousItems”. Each of those elements has their own child elements – one for each game item object.

We store the item’s property values as attributes. When we get to objects that have List properties (like the Ingredients property in the Recipes class), we will make an element for each recipe, which will have a child element for each ingredient.


Another popular format is JSON (JavaScript Object Notation). If you ever want to use JSON in a .NET program, you will probably want to include the Newtonsoft.Json NuGet package in your project, for its helper functions.





Step 3: Modify Engine\Factories\ItemFactory.cs

Now, we’ll change the ItemFactory class to read from the GameItems.xml file.


We need to add three new “using” statements, since we are using code from new namespaces (for the XML, reading the file, and converting values). Add:


On line 13 is a constant to hold the data file name. The single “.” means, “start looking for this file from the current directory” (the directory where the program will be running.

We need to double the “\” characters, because a single “\” in a string is normally used to insert special characters, like tabs, carriage returns, and line feeds. This is called an “escape sequence“. By having “\\” C# knows we just want to include the “\” character in the string.


I’ve changed the ItemFactory function from the hard-coded values. Now, it checks if the data file exists. If it does, the function creates an XmlDocument object, reads all the text from the data file, and loads the data into the XmlDocument object. Now we can use the XML functions to retrieve data from the XML elements.

This calls the LoadItemsFromNodes function that accepts a list of XML nodes (elements). The parameter passed into “SelectNodes” is the XPath of the nodes we are looking for in the XmlDocument.

So, “/GameItems/Weapons/Weapon” finds all the nodes named “Weapon” that are children of the node named “Weapons”, which is a child of the node named “GameItems”.


The LoadItemsFromNodes function (lines 44-78) loops through each node in the passed-in parameter (an XmlNodeList object), gets the values from the node’s attributes, and creates an object to add to the _standardGameItems variable.


DetermineItemCatagory (lines 80-91) looks at the Name property of the current node to determine the value for the object’s ItemCategory property.


To read the attribute values, there are three new functions on lines 93-113: GetXmlAttributeAsInt, GetXmlAttributeAsString, and GetXmlAttribute.

GetXmlAttribute looks in the node’s Attributes collection (line 105) for the attribute with the name we’re looking for. If it finds it, it returns the attribute’s Value as a string (line 112).

The two extra functions are to get the values back as either a string or an integer. We’re going to clean this up in the upcoming lesson on generics.


The rest of the code in LoadItemsFromNodes reads values from the node’s attributes and uses them to populate the GameItem object’s properties.

On lines 62-68, we build the AttackWithWeapon action for weapons. On lines 69-74, we build the Heal action for healing items.





Step 4: Test the game


Return to main page

Leave a Reply

Your email address will not be published. Required fields are marked *