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 version="1.0" encoding="utf-8"?>
-
<DatabaseConfig>
-
<Data>
-
<DatabaseConfigData
-
File="Data/Principal.xml"
-
EntityName="Principal"
-
Format="Xml" />
-
<DatabaseConfigData
-
File="Data/Role.xml"
-
EntityName="Role"
-
Format="Xml" />
-
</Data>
-
</DatabaseConfig>
And if you have the following classes (C#):
-
public class DatabaseConfig
-
{
-
public DatabaseConfigData[] Data { get; set; }
-
};
-
-
public class DatabaseConfigData
-
{
-
[System.Xml.Serialization.XmlAttribute()]
-
public string File { get; set; }
-
-
[System.Xml.Serialization.XmlAttribute()]
-
public string EntityName { get; set; }
-
-
[System.Xml.Serialization.XmlAttribute()]
-
public string Format { get; set; }
-
};
All you have to do to load the XML information into the DatabaseConfig class is (C#):
-
DatabaseConfig GetDatabaseConfig(string file)
-
{
-
XmlSerializer serializer =
-
-
using (StreamReader reader =
new StreamReader
(file
))
-
{
-
object obj = serializer.Deserialize(reader);
-
return (DatabaseConfig)obj;
-
}
-
}
There are a few issues you have to handle when using XML Serialization, wich you can see at my earlier posts about XML, XSD and Serialization.