<Room>
<RoomID>1</RoomID>
<RoomDirections>North,South</RoomDirection>
</Room>
<Room>
<RoomID>2</RoomID>
<RoomDirections>West, East</RoomDirection>
</Room>
</Rooms>
Lets say this is my XML file and I want to get the RoomDirection to be sit into an enum array for each room class I have, how do I turn these strings from XML to an Enum Array ?
Class Room {
int ID = ....
enum[] Directions
}
MacDx
March 28, 2018, 8:22pm
2
There are many ways to read from an XML file. You could create your own parser or use .NET which provides many different classes to do this.
One way to do it is using the XmlDocument class inside the System.Xml namespace (This is one of the ways to do it with .NET).
Here’s an example:
string xmlFilePath = Application.dataPath + "/file.xml";
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(xmlFilePath);
XmlNodeList nodeList = xmlDoc.DocumentElement.SelectNodes("/Rooms/Room"); //Gets all Room nodes
foreach (XmlNode node in nodeList)
{
Debug.Log(node.SelectSingleNode("RoomDirections").InnerText);
}
Then you could use the Enum.Parse() method to convert the string into your desired enum.
Here’s some documentation on other Xml reading techniques:
Reading an XML file means that we are reading the information embedded in XML tags in an XML file , Reading Xml with XmlReader , Reading XML with XmlTextReader , Reading XML with XmlDocument
Hope this helps!
Note: There’s an error in the xml you posted. The closing tags for RoomDirections are missing an “s ”.