Home > Articles > Web Services > XML

Converting XML Nodes into Objects

  • Print
  • + Share This
Paul Kimmel explains a seldom-documented but useful technique for reconstituting an XML node in a web.config or app.config file into an object. This technique is used by Microsoft programmers in valuable tools such as the Exception Management Application Block (EMAB) to permit you to treat an XML node as a persisted XML object.
Like this article? We recommend

Introduction

By now you may know that an app.config or a web.config file can be added to a .NET application. The .config file supports externalizing application settings where in the past you might have used the registry. You probably also know that the ConfigurationSettings.AppSettings property permits you to read individual values from a node named <appSettings> that you have to add. The <appSettings> node requires that you add name-and-value pairs, and the values can be read by indexing the AppSettings property with the name, returning the string value. In essence, this feature of .NET is like an object referred to as a dictionary. But sometimes you may need or want more than a dictionary object from your XML configuration files.

If you want a strongly typed object, or to treat an XML node as a manually persisted object, or you want custom validation and behaviors associated with the values in an XML node, you want your own configuration section handler. A configuration section handler is literally any class that realizes IConfigurationSectionHandler and is coded to know how to read an XML node and return an instance of an object. Both the XML node and how to read it are defined by you. However, the System.Xml namespace already has the toolset; you simply need to orchestrate its use.

The XML node can be anything. I've used a configuration section handler to objectify settings for a custom FTP client, for example, and Microsoft uses it in the Exception Management Application Block (EMAB) and probably many other places. To demonstrate how to create a configuration section handler and possibly give you some ideas, this article defines a pseudo-macro language and a section handler that permits scripting an application's startup behavior from a .config file.

  • + Share This
  • 🔖 Save To Your Account