One way to create XML data structures from game objects and properties is by hand-coding a method to create each element and its contents, using the XMLWriter
class.
To create XML text data using XMLWriter
, follow these steps:
// file: CreateXMLString.cs using UnityEngine; using System.Collections; using System.Xml; using System.IO; public class CreateXMLString : MonoBehaviour { private string output = "(nothing yet)"; private void Start () { output = BuildXMLString(); } private void OnGUI() { GUILayout.Label( output ); } private string BuildXMLString() { StringWriter str = new StringWriter(); XmlTextWriter xml = new XmlTextWriter(str); // start doc and root el. xml.WriteStartDocument(); xml.WriteStartElement("playerScoreList"); // data element xml.WriteStartElement("player"); xml.WriteElementString("name", "matt"); xml.WriteElementString("score", "200"); xml.WriteEndElement(); // data element xml.WriteStartElement("player"); xml.WriteElementString("name", "jane"); xml.WriteElementString("score", "150"); xml.WriteEndElement(); // end root and document xml.WriteEndElement(); xml.WriteEndDocument(); return str.ToString(); } }
The Start()
method calls BuildXMLString()
and stores the returned string in the output
variable. Our OnGUI()
method displays the contents of output
.
The BuildXMLString()
method creates a StringWriter
object, into which XMLWriter
builds the string of XML elements. The XML document starts and ends with the WriteStartDocument()
and WriteEndDocument()
methods. Elements start and end with
WriteStartElement(<elementName>)
and
WriteEndElement()
. String content for an element is added using WriteElementString()
.
Some details you don't want to miss:
After every instance of the WriteStartElement()
and WriteElementString()
methods, you can add a newline character using WriteWhiteSpace()
. These are ignored by XML parsing methods, but if you intend to display the XML string for a human to see, the presence of the newline characters makes it much more readable:
xml.WriteWhitespace(" ");
The XML to be generated is often from a list of objects, all of the same class. In this case, it makes sense to make the class of the objects responsible for generating the XML for a list of those objects.
The CreateXMLFromArray
class simply creates an instance of ArrayList
containing PlayerScore
objects, and then calls the (static) method ListToXML()
, passing in the list of objects.
// file: CreateXMLFromArray.cs using UnityEngine; using System.Collections; public class CreateXMLFromArray : MonoBehaviour { private string output = "(nothing yet)"; private ArrayList myPlayerList; private void Start () { myPlayerList = new ArrayList(); myPlayerList.Add (new PlayerScore("matt", 200) ); myPlayerList.Add (new PlayerScore("jane", 150) ); output = PlayerScore.ListToXML( myPlayerList ); } private void OnGUI() { GUILayout.Label( output ); } }
All the hard work is now the responsibility of the PlayerScore
class. This class has two private variables for the players' name and score and a constructor that accepts values for these properties. The public static
method ListToXML()
takes an ArrayList
object as an argument, and uses XMLWriter
to build the XML string, looping through each object in the list and calling the object's ObjectToElement()
method. This method adds an XML element to the XMLWriter
argument received for the data in that object.
// file: PlayerScore.cs using System.Collections; using System.Xml; using System.IO; public class PlayerScore { private string _name; private int _score; public PlayerScore(string name, int score) { _name = name; _score = score; } // class method static public string ListToXML(ArrayList playerList) { StringWriter str = new StringWriter(); XmlTextWriter xml = new XmlTextWriter(str); // start doc and root el. xml.WriteStartDocument(); xml.WriteStartElement("playerScoreList"); // add elements for each object in list foreach (PlayerScore playerScoreObject in playerList) { playerScoreObject.ObjectToElement( xml ); } // end root and document xml.WriteEndElement(); xml.WriteEndDocument(); return str.ToString(); } private void ObjectToElement(XmlTextWriter xml) { // data element xml.WriteStartElement("player"); xml.WriteElementString("name", _name); string scoreString = "" + _score; // make _score a string xml.WriteElementString("score", scoreString); xml.WriteEndElement(); } }
3.142.173.238