text: The .NET Framework provides many ways to track application data, with its inherent XML support providing the perfect vehicle for maintaining this data. Take a closer look at storing application data in an XML file.
  I was working with developers recently who are new to the .NET environment, and they were scratching their heads at the concept of storing configuration information. They were stuck on the older concept of using initialisation (ini) or text files. I steered them away from this avenue toward a more appropriate .NET avenue via XML. While both ASP.NET Web Forms and Windows Forms provide configuration files for storing application data, you can always fall back on one of the main features of .NET: XML. Let's take a closer look at storing application data in an XML file.
  XML is the key
  XML is the backbone of the many technologies involved in the Web services movement, as well as being a standard feature of the .NET Framework. With that in mind, we can easily take advantage of XML and related features to store application-specific data. The process begins with defining the structure or details of the data we will be using.
  A structure or class can be created to work with the data. In our simple example, we will be storing the application name, window title, and some text entered by the user. The values are maintained via class properties. You could use the C# class that's in Listing A.
  Listing A
  One key aspect of this code is that the class is serialisable, so this makes it possible to serialise an instance of the class to disk, thus maintaining its state. Next, we utilise this class in a very basic Windows Forms application. The class properties are used to populate two label controls. In addition, the user may enter text via a text field. The data entered in the text field is saved by way of the CustomText property. Listing B contains the sample C# code.
  Listing B
  Listing C contains the equivalent VB.NET code follows with the class presented first.
  Listing C
  Listing D features the application code.
  Listing D
  Upon loading the form, the XML file is used to populate the controls on the form. If the file does not exist, it is created and populated. The form contains one button that saves the data to the XML file when clicked. Since it is a configuration file, you may need to automatically save the file when/if the form is closed or data values change.
  There are a few differences between the C# and VB.NET versions, but they are basically the same. One key difference between the two languages is case-sensitivity. VB.NET is not case-sensitive, so an underscore was added to the class member variables to differentiate from the property names whereas it isn't a problem in C#. A key aspect of this simple application is serialisation.
  Serialisation
  Serialisation allows developers to persist objects by storing them in files. This includes the objects data and state. The files may be stored on a disk drive, database, and so forth. The .NET Framework provides various namespaces for serialising objects. In this example, we utilise the System.Runtime.Serialization namespace. To serialise an object, you need to either mark the object class with the [Serializable] attribute or implement the ISerializable interface. As stated earlier, we created our Config class as Serializable by including the Serializable attribute. Serialisation was covered in greater detail in a previous column.
  Location
  Using XML to store configuration data is great, but one important issue is deciding where to store the serialised file. One option is a backend database like SQL Server. In our sample application, the root of the local C drive was used, but this may not be appealing. Microsoft recommends storing application data in one of three locations accessible with the System.Environment class: These three directories are accessed via calls to the GetFolderPath method using the three values:
  - Environment.SpecialFolder.ApplicationData: This is the directory for the current user, shared by all machines on the network.
  - Environment.SpecialFolder.CommonApplicationData: This is the directory for storing information that is shared by all users on all machines.
  - Environment.SpecialFolder.LocalApplicationData: This is the directory for the current user that is only available when logged on to this machine.
  The code snippet in Listing E displays the settings for each of these variables on my machine. This listing also contains the output for the code snippet. Note: This is only a recommendation; you may choose to use a different directory.
  Listing E
  Maintaining application data The .NET Framework provides many ways to maintain application-specific configuration data. ASP.NET and Windows Forms applications have unique configuration files with associated programming models, but you can easily take advantage of the power of XML to store and maintain configuration information.
  Do you need help with .Net? Gain advice from Builder AU forums
  1
  Steve Bailey - 29/11/06
  Thanks for this article, Tony.
  This is exactly what I needed for a small utility app I'm writing that needs just the basic two way configuration setting capability.
  Personally, I also needed to serialize an object which had, as one of it's fields, an ArrayList. So I had to research that a little further.
  For anyone who needs to do that:
  » Report offensive content
  2
  cipher_nemo - 20/09/07
  Thank you for sharing this! It has given me a very clean and simple procedure for saving and loading app settings from an XML file. Not a single mistake or oversight in this article. Perfect! :-)
  I would like to add that a developer using these should enclose the streamwriter commands in try statements. Example for the save section:
  » Report offensive content