XML is a common data exchange format; it is useful to be able to parse (process the contents of) text files and strings containing data in this format. C# offers a range of classes and methods to make such processing straightforward.
You'll find player name and score data in XML format in the playerScoreData.xml
file in the 0423_08_04
folder. The contents of this file are as follows:
<scoreRecordList> <scoreRecord> <player>matt</player> <score>2200</score> <date> <day>1</day> <month>Sep</month> <year>2012</year> </date> </scoreRecord> <scoreRecord> <player>jane</player> <score>500</score> <date> <day>12</day> <month>May</month> <year>2012</year> </date> </scoreRecord> </scoreRecordList>
The data is structured by a root element named scoreRecordList
, which contains a sequence of scoreRecord
elements. Each scoreRecord
element contains a player
element (which contains a player's name), a score
element (which has the integer content of the player's score), and a date
element, which itself contains three child elements, day
, month
, and year
.
To load and parse external XML files, follow these steps:
// file: ParseXML.cs using UnityEngine; using System.Collections; using System.Xml; using System.IO; public class ParseXML : MonoBehaviour { public TextAsset scoreDataTextFile; private void Start() { string textData = scoreDataTextFile.text; ParseScoreXML( textData ); } private void ParseScoreXML(string xmlData) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load( new StringReader(xmlData) ); string xmlPathPattern = "//scoreRecordList/scoreRecord"; XmlNodeList myNodeList = xmlDoc.SelectNodes( xmlPathPattern ); foreach(XmlNode node in myNodeList) print ( ScoreRecordString( node ) ); } private string ScoreRecordString(XmlNode node) { XmlNode playerNode = node.FirstChild; XmlNode scoreNode = playerNode.NextSibling; XmlNode dateNode = scoreNode.NextSibling; return "Player = " + playerNode.InnerXml + ", score = " + scoreNode.InnerXml + ", date = " + DateString( dateNode ); } private string DateString(XmlNode dateNode) { XmlNode dayNode = dateNode.FirstChild; XmlNode monthNode = dayNode.NextSibling; XmlNode yearNode = monthNode.NextSibling; return dayNode.InnerXml + "/" + monthNode.InnerXml + "/" + yearNode.InnerXml; } }
print()
statements should be visible in the Console window:Note the need to use the System.Xml
and System.IO
packages for this recipe.
The text
property of the TextAsset
variable scoreDataTextFile
provides the contents of the XML file as a string, which is passed to the ParseScoreXML()
method. This method creates a new XmlDocument
variable with the contents of this string. The XmlDocument
class provides the SelectNodes()
method, which returns a list of node objects for a given element path. In this example, a list of scoreRecord
nodes is requested. A for-each
statement prints out the string returned by the ScoreRecordString()
method when each node object is passed to it.
The ScoreRecordString()
method relies on the ordering of the XML elements to retrieve the player's name and score, and it gets the date as a slash-separated string by passing the node containing the three date components to the DateString()
method.
3.144.21.158