- Home /
extract data from XML file to fill enum.
<Rooms>
<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
}
Answer by MacDx · Mar 28, 2018 at 08:22 PM
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:
http://csharp.net-informations.com/xml/how-to-read-xml.htm
Hope this helps!
Note: There's an error in the xml you posted. The closing tags for RoomDirections are missing an "s".
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer
How do I use this XML reader? 1 Answer
Extract C# code from XML file 1 Answer