Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Persisting Data in Your Windows Mobile Device : Page 4

Learn about the three methods of persisting data and which is the right one for your device.


advertisement
XML
So far, you've learned that for simple storage of data, using files is the best solution while for complex structured data, a database is more appropriate. However, there are times in which both solutions are not really suitable. For example, suppose you need to store some configuration information about your application (such as users' preferences, network credentials, and so on). In such cases, using files is clumsy because you need to manually keep track of all the information you are storing (data length, fields, etc.). But using a database is overkill because the data you are storing is not repeating. A good solution to this problem is to use XML. An XML document allows you to store the information in a context-rich format. At the same time, you do not need to worry about the nitty-gritty details of retrieving and storing the data—such things can be taken care of by .NET Compact Framework's XML classes.

Listing 4 shows how you can create an XML file named config.xml and create a root element named <Configuration> with two sub-elements: <LastAccess> and <LastSearchString>.

When executed, the content of the config.xml file would look like this:



<Configuration> <LastAccess>5/12/08 10:32:21 PM</LastAccess> <LastSearchString>sushi</LastSearchString> </Configuration>

To load the XML document into memory, simply use the XmlDocument class' Load() method:

XmlDocument xml = new XmlDocument(); xml.Load(configFile);

To retrieve the contents of the <LastAccess> and the <LastSearchString> elements, use the XmlDocument class's SelectSingleNode() method by passing in an XPath expression for each method, like this:

//---XPath expression 1--- XmlNode node = xml.SelectSingleNode("Configuration/LastAccess"); MessageBox.Show(node.InnerText); //---XPath expression 2--- node = xml.SelectSingleNode("//LastSearchString"); MessageBox.Show(node.InnerText);

Note that the above methods are useful if your XML document is relatively simple. If you have a large and complex XML document, you'd spend a lot of time writing XPath expressions to look for the relevant elements to retrieve.

An improved method is to use XML serialization. config.xml could be appropriately represented using a class. For example, you can define the Configuration class to look like this:

public class Configuration { public DateTime LastAccess { get; set; } public string LastSearchString { get; set; } }

To store the last access date and the last search string, create an instance of the Configuration class:

Configuration config = new Configuration() { LastAccess = DateTime.Now, LastSearchString = "sushi" };

When you are ready to store the last access date and last search string to an XML document, serialize the object using XML serialization:

string configFile = @"config.xml"; StreamWriter sw = new StreamWriter(configFile); try { XmlSerializer s = new XmlSerializer(typeof(Configuration)); s.Serialize(sw, config); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } finally { sw.Close(); }

The above code uses the XmlSerializer class to serialize an object (a Configuration object) and then writes the output to an XML file using the StreamWriter class. The config.xml file will then look like this:

<Configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <LastAccess>2008-05-12T23:28:13-07:00</LastAccess> <LastSearchString>sushi</LastSearchString> </Configuration>

To read the XML document back into a Configuration object, you will reverse the process, that is, you will now de-serialize the object. The following code uses the XmlReader class to read the config.xml file and then uses the XmlSerializer class to de-serialize the XML content read by the XmlReader object:

Figure 6. Reading config.xml: The content read from the de-serialization of the XML document.

config = null; XmlReader xr = XmlReader.Create(configFile); try { XmlSerializer s = new XmlSerializer(typeof(Configuration)); config = (Configuration)s.Deserialize(xr); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } finally { xr.Close(); } MessageBox.Show(config.LastAccess.ToString()); MessageBox.Show(config.LastSearchString);

The above code will print out the message box shown in Figure 6.

Three Methods, Lots of Flexiblity
As a quick recap, here are your options for persisting data to your Windows Mobile devices:

  • File: This method is useful for storing simple data such as error logs, comments, etc.
  • Database: This method is useful for storing large amount of structured data, such as customer records, products list, etc.
  • XML Documents: This method is useful for storing data that needs to be read and written frequently, such as configuration information, user credentials, etc. In particular, XML serialization is extremely useful for storing configuration information.



Wei-Meng Lee is a Microsoft MVP and founder of Developer Learning Solutions, a technology company specializing in hands-on training on the latest Microsoft technologies. He is an established developer and trainer specializing in .NET and wireless technologies. Wei-Meng speaks regularly at international conferences and has authored and coauthored numerous books on .NET, XML, and wireless technologies. He writes extensively on topics ranging from .NET to Mac OS X. He is also the author of the .NET Compact Framework Pocket Guide, ASP.NET 2.0: A Developer's Notebook (both from O'Reilly Media, Inc.), and Programming Sudoku (Apress). Here is Wei-Meng's blog.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap