How to load XML Content at Runtime?
Describes how to load custom game data at game runtime through the Content Pipeline.
This example concludes the procedure begun in the tutorial Adding an XML Content File to a Visual Studio Project.
Once custom game data is integrated as game content from an XML file through the Content Pipeline, it exists within your game runtime package in binary format. The data can be loaded at runtime through the ContentManager.
Important
This tutorial assumes you are using Visual Studio as your IDE, for VSCode please adapt the IDE interactions appropriately.
Add a SpriteFont for testing
While not essential for loading XML, in order to demonstrate the loaded data and write it to the screen, we will add a SpriteFont to enable the drawing of strings.
In the
Solution Explorer
, Double-click theContent.mgcb
in theContent
folder to open the MGCB editor.Tip
If you have any issues opening the MGCB content project, please refer to the How to load content guide.
Click on
Edit -> New Item
, then name the fileFont
and selectSpriteFont Description
as the type. Click onCreate
to finish.Save the content project and then continue.
To load the custom data in the game
To add the
MyDataTypes
library as a reference in the game project, In theSolution Explorer
, right-click the game project, clickAdd Reference
, click theProjects
tab, selectMyDataTypes
, and then clickOK
.Then in the
Solution Explorer
, double-clickGame1.cs
to edit it.Add the
using
declaration for the MyDataTypes namespace at the top of the file beneath the existingusings
statements.using MyDataTypes;
Add a data declaration for an array of type
PetData
after the otherprivate
variables in theGame1
class.private PetData[] pets;
In the Game.LoadContent override function, load the custom content.
protected override void LoadContent() { // Load the pet data table pets = Content.Load<PetData[]>("pets"); }
The custom game data now resides in the array of
PetData
objects.The data loaded from the XML file into the
PetData
array can be accessed normally as c# code, for example, the followingDraw
code will render the data from the XML file to the screen if found, else it will write a warning.protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); _spriteBatch.Begin(); // Check if the XML data was loaded into the pets data array. if (pets != null) { // For each pet, write its data to the screen. for (int i = 0; i < pets.Length; i++) { var pet = pets[i]; _spriteBatch.DrawString(Content.Load<SpriteFont>("Font"), $"{pet.Name} / {pet.Species}: Weight [{pet.Weight}] - Age: [{pet.Age}]", new Vector2(100, 100 + 20 * i), Color.White); } } else { // If no data was loaded (or there was an error) write `No Pets found` to the screen. _spriteBatch.DrawString(Content.Load<SpriteFont>("Font"), "No pets found", new Vector2(100, 100), Color.White); } _spriteBatch.End(); base.Draw(gameTime); }
The result of loading the XML and rendering the data should display as follows: