Click here to Skip to main content
15,867,308 members
Articles / Mobile Apps / Windows Mobile

Compact Framework Configuration XML File Read & Write like appSettings

Rate me:
Please Sign up or sign in to vote.
3.00/5 (1 vote)
13 Jan 2010CPOL2 min read 36.7K   922   20   5
How to use an XML file to read and write aplication settings in a Window Mobile development
CFNET-AppSettings

Introduction

The code provided her is to use XML files as a storage for data (fast).
It is useful to use like the .NET Framework appSettings class, but devoted to the compact framework. It is 100% compatible and very lightweight (compared to SQLCE). It solves the problem of storing data permanently in any CFNet device, related to an application -not only settings-, so you can use this class to store other kinds of data in the client Windows Mobile smartdevice.

Background

This is easily portable to C# This code -for VB.NET- create an XML file if no one's present. It's very easy to use in less than a minute because you have a default XML settings file that is on a string variable that you can easily customize (inside #MyAppSetting.vb). With that class, you can create/store, load and update data through XML. You can add all the 'data fields' that your application requires and manage in simple 1 or 2 ways:

  • Fast: Writing that data fields required directly on the variable string that contains the XML as text (inside #MyAppSetting.vb)
  • With integrated intellisense: just by writing 1 property per data or setting you need, as exposed below:
VB.NET
'Within class
Public Property YourRequiredData() As TypeNeeded
    Get
        Return Me.ListItems.Get("YourRequiredData")
    End Get
    Set(ByVal value As TypeNeeded)
        Me.ListItems.Set("YourRequiredData", value)
    End Set
End Property

Using the code

It is very simple to use, and it is well documented (I think). Just add the class MyAppSetting.vb to your project, then:

VB.NET
'At the top of the form, just below form class declaration
Dim MyAppSettings As New MyAppSettings

To the above instance, you can pass an optionally args; Path string and a FileName string to look for/create (look at the supplied code for more info). If the class did not find any XML file, then automatically create one, just ready to use. What? The default XmlDefaultConfig.... In the class there a var that holds the default XML configuration that you can customize (all custom code inside appSettings tab, respecting the format):

XML
Private XmlDefaultConfigurationFileString As String = _
"<?xml version=""1.0"" encoding=""utf-8"" ?>" & _
"<configuration>" & _
    "<appSettings>" & _
        "<add key=""FileCreated"" value=""" & Now & """ />" & _
        "<add key=""FileModified"" value="""" />" & _
        "<add key=""UserName"" value="""" />" & _
        "<add key=""LoginCount"" value=""0"" />" & _
    "</appSettings>" & _
"</configuration>"

Now you can load data anywhere:

VB.NET
'Code to Load data
Dim YourRequiredData As AnyType = MyAppSettings.YourRequiredData 'if you write that 
			'property/per/ as indicated above at the top of document 
Dim YourRequiredDataWithoutTypeAnyProp As AnyType = _
	CType(MyAppSettings.ListItems("YourRequiredDataWithoutTypeAnyProp"), AnyType)

You can save by doing:

VB.NET
'Code to Save/Update data to xml settings
MyAppSettings.YourRequiredData = "new data to store .."
MyAppSettings.ListItems("YourRequiredDataWithoutTypeAnyProp") = Now.today 'for example
MyAppSettings.SaveXmlCurrentConfiguration()

Points of Interest

I write 3 different methods to save the XML default file (that you could implement for update or save in another method). They are in the following enum:

VB.NET
 Enum enumXmlSaveMethod
    StreamWrite = 1     'Basic
    XmlTextWriter = 2   'Good
    XmlDocument = 3     'Good as knowledge

End Enum                
Private XmlSaveMethod As enumXmlSaveMethod = enumXmlSaveMethod.XmlDocument

You can try the 3 just by changing the last assign.

History

All that remains is to apply the 3 different methods to save -if you want- for the:

VB.NET
Public Function SaveXmlCurrentConfiguration_
	(Optional ByVal StrPath As String = Nothing, _
	Optional ByVal StrFilename As String = "AppSettings.xml")

now only applies to:

VB.NET
Public Function SaveXmlDefaultConfigurationFile(ByVal FilePath As String, _
	Optional ByVal XmlConfigurationFileString As String = Nothing) As Boolean 

I only applied to one sub for build and show less code to allow the developer to see and choose.

This is my first entry that I built for the CodeProject community, around 4 hours including testing, plus near 2 hours to document and upload. I'll write that code -based on others including pbrroks at SaveSettings.aspx, and Microsoft at http://msdn.microsoft.com/en-us/library/aa446526.aspx, ..-, to get our mobile project work fast with config.data.

I hope you like it. - Please post any bugs if you troubleshoot!
Alejandro B. Martin

History

  • 13th January, 2010: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Spain Spain
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionerror opening project Pin
coppola_f6-Nov-14 8:31
coppola_f6-Nov-14 8:31 
GeneralMy vote of 3 Pin
hjgode1-Mar-11 19:47
hjgode1-Mar-11 19:47 
GeneralFile OK! Updated Pin
Alejandro Barrada13-Jan-10 12:18
Alejandro Barrada13-Jan-10 12:18 
GeneralCorrupt download Pin
Smitha Nishant13-Jan-10 4:29
protectorSmitha Nishant13-Jan-10 4:29 
GeneralRe: Corrupt download Pin
Alejandro Barrada13-Jan-10 11:50
Alejandro Barrada13-Jan-10 11:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.