Game Development Reference
In-Depth Information
Next, we created the Parse(string xml) funcion. This funcion will create the
XmlDocument and we use LoadXml(xml) to load the string XML that we pass to this
funcion. Then, we get the XmlNode from the last child of the XmlDocument :
root = doc.LastChild;
We used LastChild() because we want to skip the first node, which is the headline of
the XML file <?xml version="1.0" encoding="UTF-8"?> . Ater we got the root
XmlNode , we checked for the child in this node, assigned the number of its children, and
created the array to store username and score data from this node:
//Getting the Node Length
userLength = root.ChildNodes.Count;
names = new string[userLength];
scores = new int[userLength];
Then, we loop to all the children, get the attribute of each child,
and store it to names[] and scores[] array by using the script below:
for (int i = 0; i < userLength; i++) {
//Getting the user name and score XmlAttribute
XmlAttribute nameAtt = root.ChildNodes[i].Attributes["name"];
XmlAttribute scoreAtt = root.ChildNodes[i].
Attributes["score"];
//Assigning the user name data to array
names[i] = (string)nameAtt.Value;
//Assigning the user score data to array
scores[i] = ConvertStringtoInt((string)scoreAtt.Value);
}
Since scoreAtt.Value is a string and we want to store it as an integer, we need to convert
the string data to an integer by creaing the funcion that will convert the string type to
int type, which we call ConvertStringtoInt( string s) .
private int ConvertStringtoInt( string s) {
int j;
bool result = System.Int32.TryParse(s, out j);
if (true == result) {
return j;
} else {
Debug.Log("Error...");
return 0;
}
}
 
Search WWH ::




Custom Search