Game Development Reference
In-Depth Information
The using System.Xml allows us to access the System.
Xml library in the .NET Framework. If you have any experience with
AcionScript, this is similar to import flash.something . The
using System.Xml will include the XmlDocument and XmlNode
object, which we will use in our XMLParser script.
2. Then, we will replace all the rest with the following script:
public static class XMLParser {
private static XmlDocument doc;
private static static XmlNode root;
private string[] names;
private static int[] scores;
private static int userLength;
3. Next, we will add the Parse() funcion to parse the XML string. Let's type it
as follows:
public static void Parse( string xml) {
doc = new XmlDocument();
doc.LoadXml(xml); // Loading from String
//Using doc.Load("HiScore.xml"); When load from an xml file
//Using Last Child to Skip the <?xml version="1.0"
encoding="UTF-8"?>
//If we load from the xml file we will use the FirstChild
instead
root = doc.LastChild;
if (root.HasChildNodes) {
//Getting the Node Length
userLength = root.ChildNodes.Count;
names = new string[userLength];
scores = new int[userLength];
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);
}
}
}
 
Search WWH ::




Custom Search