|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Services
Chapters
Feature Zones
|
IntroductionThis article describes how to implement custom objects in the App.Config file, enabling data to be stored in XML format, and to be easily deserialized into a custom object. BackgroundThere is an assumption that the reader is reasonably familiar with XML Serialization from the Using the codeFor this example, I have created two classes that will be configured in the App.Config file. The first is a simple class designed to illustrate how to configure a simple class for deserialization, the second is more complex, and involves a collection class as well. I've included the second class and associated classes in the example code, but only describes the first class. The extra complexity is not related to custom configuration handling, but more to XML Serialization, so I leave it up to the reader to read the code in conjunction with the XML Serialization documentation for a better understanding. For the first example, I have created a class called
The first two are implemented as public strings, the third uses property assessors. This is a better way to go, I included the first two to illustrate how Serialization works. As long as the element has the same name, it will be found, and it doesn't matter how you implement the property. The first piece of code to note in the App.Config file is the element telling the CLR that we have a custom configuration section. It is in an element called <configuration>
<configSections>
<section name="ProgramInfo"
type="ConfigSectionHandler.ConfigSectionHandler, ConfigSectionHandler" />
</configSections>
</configuration>
The main element of interest is the Before we look at the <ProgramInfo type="ConfigSectionObjects.ProgramInfo, ConfigSectionObjects">
<Name>ConfigSectionDemo</Name>
<Language>C#</Language>
<Level>Intermediate</Level>
</ProgramInfo>
This is straightforward XML, but note the Let's look at an abbreviated version of the namespace ConfigSectionObjects
{
public class ProgramInfo
public ProgramInfo() {
}
public string Name;
public string Language;
private string _Level;
public string Level
{
get {return _Level;}
set {_Level=value;}
}
}
A fairly simple class. I implemented the three properties in two different ways, the first two use public variables, which is not really a good way to handle property access. The last property uses accessor methods. Note: the combination of the Namespace and the Class name gives us The class name is the same as the element name in the custom section. The two public variables and the one public property map directly to the children of the At this point, we have specified in our App.Config file all the information we need, and we have also defined the class that will be deserialized from the App.Config file. Now, we need to use some code to handle the deserialization. To do this, we need to use the ConfigSectionObjects.ProgramInfo pi=
(ConfigSectionObjects.ProgramInfo)ConfigurationSettings.GetConfig("ProgramInfo");
We declare a variable of the Console.WriteLine ("Program: {0}", pi.Name);
Console.WriteLine ("Language: {0}", pi.Language);
Console.WriteLine ("Level: {0}", pi.Level);
Now, it is worth looking at the actual
It is only
The code follows: XPathNavigator xNav=section.CreateNavigator();
string typeOfObject=(string) xNav.Evaluate("string(@type)");
Type t=Type.GetType(typeOfObject);
XmlSerializer ser=new XmlSerializer(t);
XmlNodeReader xNodeReader=new XmlNodeReader(section);
return ser.Deserialize(xNodeReader);
Complex SerializationIt is possible to do more complex serialization. For example, in the [XmlElement("Name")]
public string Description;
This is telling the deserializer, that when the Alternatively, if rather than having [XmlAttribute("Level2")]
public string Level2;
Once again, refer to the example code, the Points of InterestI found I made a lot of errors when defining the I also found the documentation very vague when it came to configuration files. History
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||