If you want to store configuration information in a XML file, you can easily use serialization to assist you. Instead of using XPath or DOM, and have some code to maintain, simply use the XmlSerializer to retrieve information.

Here's an example of a XML file:

XML:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <DatabaseConfig>
  3.   <Data>
  4.     <DatabaseConfigData
  5.       File="Data/Principal.xml"
  6.       EntityName="Principal"
  7.       Format="Xml" />
  8.     <DatabaseConfigData
  9.       File="Data/Role.xml"
  10.       EntityName="Role"
  11.       Format="Xml" />
  12.   </Data>
  13. </DatabaseConfig>

And if you have the following classes:

C#:
  1. public class DatabaseConfig {
  2.     public DatabaseConfigData[] Data { get; set; }
  3. };
  4.  
  5. public class DatabaseConfigData {
  6.     [System.Xml.Serialization.XmlAttribute()]
  7.     public string File { get; set; }
  8.  
  9.     [System.Xml.Serialization.XmlAttribute()]
  10.     public string EntityName { get; set; }
  11.  
  12.     [System.Xml.Serialization.XmlAttribute()]
  13.     public string Format { get; set; }
  14. };

All you have to do to load the XML information into the DatabaseConfig class is:

C#:
  1. DatabaseConfig GetDatabaseConfig(string file)
  2. {
  3.     XmlSerializer serializer =
  4.         new XmlSerializer(typeof(DatabaseConfig));
  5.     using (StreamReader reader = new StreamReader(file)) {
  6.         object obj = serializer.Deserialize(reader);
  7.         return (DatabaseConfig)obj;
  8.     }
  9. }

Related Posts

5 Responses to “Best way to load XML configuration”

  1. mcgurk Says:

    I’m using xml serialization to store configuration information in a database. There are issues you have to handle when using xmlserialization for this process

    1) Any property you wish serialized must have both public getters and setters, which means you must violate some OO principles to use it with raw objects; or you could
    2) Ape the Enterprise Library method and have objects contain their configuration within a private object. That object can have all public getters and setters, as it is hid from the outside world. You can then serialize and deserialize that configuraiton object. Just keep in mind that
    3) XmlSerializer creates private assemblies in temp directories, which may cause issus in medium trust environments or when the user isn’t admin and
    4) Some simple objects are not automatically serializable, such as dictionaries and hashtables. You have to create proxy properties for XmlSerialization and mark the dictionary properties with the XmlIgnoreAttribute.

  2. mcgurk Says:

    Oh, and because XmlSerialization creates temporary assemblies, you might want to cache your serializers in a Dictionary if your process is particularly long running or if you are short on disk space.

  3. Pedro Santos Says:

    Yes, macgurk, you’re right. But for more complex object graphs, I use an IFormatter.

  4. Jon H Says:

    McGurk hit on a sore spot with me, which is the lack of being able to use XMLSerialize with IDictionary types.

    I don’t understand why this was not implemented, and I’m pretty tired of writing proxy objects for it :)

  5. Uday Garikapati Says:

    Good work from C# side.
    But be careful whenever your XML structure changes you have to modify your class. But it’s easy attempt from parsing xml files and having nitemare…

    It;s already there in Java world called Apache Commons Digester…

    Cheers !