Game Development Reference
In-Depth Information
Objective complete - mini debriefing
In this section, we basically just created the
XMLParser
script to parse the XML string
that we loaded from the server, and then we stored the user's data in this class to use it at a
later stage.
First, we used the
static
keyword for this class because we wanted it to be accessible
from the entire project. Then, we created the
XmlDocument
and
XmlNode
parameters to
hold the XML data that we want to parse. Then, we had the array of
UserData
to contain
all the user data. The last parameter is to store the length of the users that we have got from
the XML data.
Next, we created the
Parse(string xml)
function. In this function, we created
Xm-
lDocument
and then loaded the XML string data of this document using
_
doc.LoadXml(xml)
to load the string XML that we pass from the server. Then, we get
XmlNode
from the last child of
XmlDocument
as follows:
_root = _doc.LastChild;
We used
LastChild()
because we wanted to skip the first node, which is the headlineof
the
<?xml version="1.0" encoding="UTF-8"?>
XML file. Next, we checked
if the
_root
node has a child node or not. If it has, we get the length of this child node.
Then, we created the array of
UserData
to store username and score data.
Next, we looped through the child node, got the name and score from the XML attribute,
set it to the
UserData
object, and then we added the data to the array of
UserData
.