XML InvalidOperationException

Currently attempting to load data from an XML file but I am having trouble.

The script trying to access the XML file looks like this:

using UnityEngine;
using UnityEditor;
using System.Collections;
using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
 
[Serializable]
[XmlRoot("Monster")]
public class monsterData {
    [XmlAttribute("MonsterID")]
    public int monsterID;       
    public string Name;
    public int Health;
    public int Attack;
}

public class Testing : MonoBehaviour {
	public int monsterID;
	public string monsterName;
	public int attack;
	public int health;

	public void LoadXML() {
		if (File.Exists(Application.dataPath + "/Database/MonsterData.xml")) {
			Debug.Log("File Found");
			var serializer = new XmlSerializer(typeof(monsterData));
			using(var stream = new FileStream(Application.dataPath + "/Database/MonsterData.xml", FileMode.Open))
			{
				monsterData data = (monsterData)serializer.Deserialize(stream) ;
				this.monsterID= data.monsterID;
				this.monsterName = data.Name;
				this.attack = data.Attack;
				this.health = data.Health;
			}
		} else {
			Debug.Log("File not found");
		}
	}
		
	void Start() {
		LoadXML();
	}
}

and the XML file I’m trying to load values from looks like this:

<?xml version="1.0"?>
<Monsters>
    <Monster MonsterId = 1>
		<Name>"Green Slime"</Name>
        <Health>20</Health>
        <Attack>15</Attack>
    </Monster>
	<Monster MonsterId = 2>
		<Name>"Blue Slime"</Name>
        <Health>30</Health>
        <Attack>10</Attack>
    </Monster>
</Monsters>

It seems that you must insert the array Monsters into a root class. Take a look at the first and the fourth answers to this question in stackoverflow: the first answer is more complicated but gives some clues, and the fourth answer (by erymski) is a more elegant and way more simplified version.

Your class structure can 100% reflect the xml document structure. The properties can explicitly be used to determine names of elements or attributes, or you can specify them in the xml attributes constructor as you’ve done already.

[Serializable]
[XmlRoot("Monsters")]
public class monstersData {
	[XmlElement()]
	public List<monsterData> Monsters { get; set; }
}

[Serializable]
public class monsterData {
	[XmlAttribute()]
	public int MonsterId { get; set; };
	
	[XmlElement()]
	public string Name { get; set; };
	
	[XmlElement()]
	public int Health { get; set; };

	[XmlElement()]
	public int Attack { get; set; };
}

@Suppish I know it is a long time ago but since the question is open and someone may need the answer, you are trying to serialize a List of Monsters your code may work for one monster so you need a wrapper class Monsters that has a public property List MonsterGroup the XMLElement not XMLArray are needed for primetives types the Class/XML structure looks like this:

 [XmlRoot("Monster"), Serializable]
 public class monsterData {
     [XmlAttribute("MonsterID")]
     public int monsterID;       
     public string Name;
     public int Health;
     public int Attack;
 }

[XmlRoot(ElementName ="Monsters ",Namespace =""), Serializable]
public class Monsters {
    [XmlArray("MonstersGroup"), XmlArrayItem(typeof(monsterData ), ElementName = "Monster")]
    public List<monsterData> MonstersGroup = new List<monsterData>();
}

<?xml version="1.0"?>
 <Monsters>
     <MonsterGroup>
         <Monster MonsterId = 1>
            <Name>"Green Slime"</Name>
            <Health>20</Health>
            <Attack>15</Attack>
        </Monster>
     </MonsterGroup>
 </Monsters>