Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Community Wiki

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Navigation

Google Ads

Serialize an Object to XML in CSharp

From Wiki

Jump to: navigation, search

Every once in a while we all find the need to persist objects to local storage. Access databases and even SQL CE can be a bit much for such a simple task, so sometimes a small XML file will do the trick. Most recently I've used this to store different configurations that an application uses at runtime. This class uses generics so that it can serialize and deserialize any type. Here is the code:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.IO;
  5. using System.Xml;
  6. using System.Xml.Serialization;
  7. using System.Reflection;
  8.  
  9.  
  10.  
  11. namespace MyApp
  12. {
  13.     class XMLClassMapper<T> where T: new()
  14.     {
  15.         private XmlSerializer _serializer;
  16.         private FileStream _stream;
  17.  
  18.         public bool WriteClass(T toWrite, String fileName)
  19.         {
  20.             //FileMode.Create overwrites existing by default, be aware of this
  21.             _stream = new FileStream(fileName, FileMode.Create);
  22.  
  23.             _serializer = new System.Xml.Serialization.XmlSerializer(typeof(T));
  24.  
  25.             try
  26.             {
  27.                 _serializer.Serialize(_stream, toWrite);
  28.                 return true;
  29.             }
  30.             catch(XmlException ex)
  31.             {
  32.                 throw ex;
  33.             }
  34.             finally
  35.             {
  36.                 _stream.Close();
  37.                 _stream = null;
  38.  
  39.                 _serializer = null;
  40.             }
  41.         }
  42.  
  43.         public T ClassFromFile(string fileName)
  44.         {
  45.             T ret = new T();
  46.  
  47.             _stream = new FileStream(fileName, FileMode.Open);
  48.             _serializer = new XmlSerializer(typeof(T));
  49.  
  50.             try
  51.             {
  52.                 ret = (T)_serializer.Deserialize(_stream);
  53.             }
  54.             catch(XmlException ex)
  55.             {
  56.                 throw ex;
  57.             }
  58.             finally
  59.             {
  60.                 _stream.Close();
  61.                 _stream = null;
  62.  
  63.                 _serializer = null;
  64.             }
  65.  
  66.             return ret;
  67.         }
  68.     }
  69. }


Instantiate it like

  1. XMLClassMapper<MyType> mpr = new XMLClassMapper<MyType>();


WriteClass() saves your object to an XML file. ClassFromFile() loads your XML file into a class instance. In both cases, fileName is the full path to the file ie C:\folder\subfolder\file.xml. I hope you find this helpful.

390 Rating: 2.4/5 (14 votes cast)