How to handle multiple elements in xml?

Hi, my question is how I can create a class that can handle multiple elements in xml.
This is an example of xml code:
<?xml version="1.0" encoding="utf-8"?> <XnaContent> <Asset Type="LibrayName.MyClass"> <Text> <Value>text</Value> <PosX>20</PosX> <PosY>20</PosY> </Text> </Asset> </XnaContent>

And the class is this:
namespace LibraryName { public class MyClass { public string Text { get; set; } } }

But the class just can get a value from a one element and this is the my problem. I know that I can load a XML file from hard disk, but I want to learn how to use xml with the content pipeline. Can anyone help me?

Follow this guide: https://darkgenesis.zenithmoon.com/getting-started-with-monogame-using-xml/

It’s the best explanation you’re going to find.

I was playing around with XML ages ago before looking at that tutorial, so thought I would post my code here for you.

<?xml version="1.0" encoding="utf-8"?>
<XnaContent xmlns:ns="Microsoft.Xna.Framework">
  <Asset Type="System.Collections.Generic.List[Shared.Entities.HistoryRegion]">
	<Item><Key>1</Key><X>35</X><Y>32</Y><Color>FFFFFFFF</Color></Item>
	<Item><Key>2</Key><X>32</X><Y>62</Y><Color>FFFFFFFF</Color></Item>
	<Item><Key>3</Key><X>67</X><Y>45</Y><Color>FFFFFFFF</Color></Item>
	<Item><Key>4</Key><X>97</X><Y>29</Y><Color>FFFFFFFF</Color></Item>
	[snipped]
  </Asset>
</XnaContent>


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
using Microsoft.Xna.Framework;

namespace Shared.Entities
{
	[Serializable]
	public class HistoryRegion
	{
		public string Key;
		public int X;
		public int Y;

		[XmlElement("Color")]
		public Color Color;

		[XmlIgnore]
		public string test { get; private set; }

		public HistoryRegion()
		{
		}

		public HistoryRegion(string key, int x, int y, Color regionColor)
		{
			Key = key;
			X = x;
			Y = y;
			Color = regionColor;
		}
	}
}



using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace Shared.Utils
{
	/// <summary>
	/// Math Class that helps
	/// </summary>
	public static class ExtensionMethods
	{
		/// <summary>
		/// Deserialize an XML file into a class
		/// </summary>
		/// <code>#lt;T#gt;t1 = Serializer.XMLDeserialize#lt;T#gt;(file);</code>
		/// <typeparam name="T"></typeparam>
		/// <param name="xmlFileInfo">The fileinfo object.</param>
		/// <returns></returns>
		public static T DeserializeXML<T>(this FileInfo @this)
		{
			try
			{
				if (File.Exists(@this.FullName))
				{
					using (Stream stream = new FileStream(@this.FullName, FileMode.Open, FileAccess.Read))
					{
						var serializer = new XmlSerializer(typeof(T));
						return (T)serializer.Deserialize(stream);
					}
				}
				else
				{
					throw new Exception("File not found: " + @this.FullName);
				}
			}
			catch (Exception)
			{
				throw;
			}
		}

		/// <summary>
		/// Deserialize an XML file into a class
		/// </summary>
		/// <code>#lt;T#gt;t1 = Serializer.XMLDeserialize#lt;T#gt;("filename.xml");</code>
		/// <typeparam name="T"></typeparam>
		/// <param name="this">The XML filename.</param>
		/// <returns></returns>
		public static T DeserializeXMLFile<T>(this string @this)
		{
			var file = new FileInfo(@this);
			try
			{
				return file.DeserializeXML<T>();
			}
			catch (Exception)
			{
				throw;
			}
		}
	}
}

And then this code to LOAD the file:

_historyMapRegions = TextureManager.Content.Load<List<HistoryRegion>>(@"Xml\HistoryRegions");

Hope this helps

1 Like