XML Storage of Object State
XML Storage of Object State
Introduction
An introduction to the problem Writing an XML file
> The XMLTextWriter class > Attributes and Elements in XML > Text Encoding in XML files
Writing a PC Version
Unless you use some of
the Mobile Powertoys it is hard to see what a Smartphone application is doing I therefore decided to write a PC version of the code I can port the important bits later
XML Namespaces
To get direct access to the XML methods and
the text encoding types I have to use two namespaces: using System.Xml; using System.Text;
version of xml and the encoding being used The score element is shown as empty This is a completely legal XML document
> but it does not contain any data.
element, called highscore. I can add an attribute to the highscore element which identifies the game the score is for
Adding an Attribute
public void SaveXML ( string filename ) { XmlTextWriter writer ; writer = new XmlTextWriter (filename,Encoding.ASCII); writer.Formatting = Formatting.Indented; writer.WriteStartDocument(); writer.WriteStartElement("highscore"); writer.WriteAttributeString( "game", "Breakout"); writer.WriteEndElement(); writer.WriteEndDocument(); writer.Close(); }
Information directly about the high score data, such as the game
it applies to, should be an attribute Another use for an attribute would be as an id tag of an element, or perhaps a version number (which you can see in the header of the XML file itself)
Element Namespace
Not to be confused with the C# namespace
(although the intention is similar) Allows an element to state the context in which this element has meaning This means that two programmers using the same name for an element could ensure that people using their elements can determine the proper context/ontology
Adding a Namespace
writer.WriteStartElement("highscore", "www.mygameuri.com/highscore");
Reading XML
We are going to use the XmlDocument class
provided by .NET:
// get a new document document = new XmlDocument(); // load it from a file document.Load(filename);
XmlDocument structure
Name : highscore NamespaceURI: www.mygameuri.com/highscore game: Breakout
playername
score
Rob Miles
1234
Checking a namespace
// make sure it is in the right namespace if ( rootElement.NamespaceURI != "www.mygameuri.com/highscore" ) { Console.WriteLine ("Wrong namespace"); }
Reading an attribute
// check to see if the name is correct string gameName = rootElement.GetAttribute("game"); if ( gameName != "Breakout" ) { return false ; } Attributes are accessed by their name using the GetAttribute method
Setting Values
You can set values in an element as well There is also a method call which will save an
element (and all of its children) This can be used if you have updated values in the document that you want to save ou want
File Storage
You need to decide where to store the XML
files themselves The obvious place to put them is the same directory as the binary program On the Compact Framework you need to do a bit of work to get the location of this
XML is Fun!
No, really.. It provides a very easy way to manage
program data in a flexible and extensible manner
> For very little effort on your part