This project is read-only.

Core Library

The .NET Configuration Code Generator Core Library is a reusable class library that contains all the logic necessary for generating configuration code. It is used by the Windows Forms client and the Visual Studio custom tool extension to generate the configuration code.

Assembly name: NConfigGenerator.dll
Namespace: NConfigGenerator

The Core Library performs the following two functions:

Creating the configuration tree

A configuration tree is an object representation of the structure of the configuration XML for which you want to generate the configuration code. It is in the form of a tree structure (NConfigGenerator.ConfigTree), with each node (NConfigGenerator.ConfigNode) representing an element or element collection in the XML. The element's attributes are represented by a collection of NConfigGenerator.ConfigNodeAttribute objects within each node.

The ConfigTree class provides two static methods for creating a configuration tree:
  • ConfigTree.Create, which creates a configuration tree with just a root node, and then allows you to add the remaining node programmatically.
ConfigTree configTree = ConfigTree.Create("root");
configTree.RootNode.Attributes.Add(new ConfigNodeAttribute("root-attrib", AttributeType.String));

ConfigNode child = new ElementConfigNode(configTree.RootNode, "first-child");
child.Attributes.Add(new ConfigNodeAttribute("first-child-attrib", AttributeType.Boolean));

The above code would create a configuration tree for the following XML structure:

<root root-attrib="string value">
    <first-child first-child-attrib="True|False"/>
  • ConfigTree.InferFromXml, which infers the configuration tree structure from a given sample of the required configuration XML.
ConfigTree configTree = ConfigTree.InferFromXml(inputXml);
In the case of XML inference, the library will try to intelligently figure out the type of node (single element or collection) and the data types of the attributes based on the sample input XML.

To be inferred as a collection node, the element must appear multiple times in the XML. For example, in the following XML, the person element is considered a collection node, since it appears twice:
<family name="The Does">
        <person first-name="John" last-name="Doe"/>
        <person first-name="Jane" last-name="Doe"/>
The following rules apply when inferring the data types of attributes:
  1. If the attribute value is empty, it represents a string
  2. If the value can be parsed as a System.Int64 value, it represents an integer
  3. If the value can be parsed as a System.Double value, it represents a floating-point number
  4. If the value can be parsed as a System.Boolean value, or if the value is "Y", "N", "y" or "n", it represents a boolean value
  5. If the value can be parsed as a System.DateTime value, it represents a date/time value
  6. If the value can be parsed as a fully-qualified type name, it represents a System.Type object
  7. In all other cases, it represents a string

Generating the configuration code

Once the configuration tree has been created, the next and final step is to generate the configuration code. This is done using the Generator.Generate static method:
public static class Generator
    public static string Generate(ConfigTree tree, GeneratorOptions options);
The second parameter options is of type NConfigGenerator.GeneratorOptions and controls the generation of the configuration code. It has the following properties:
  • Language: The language of the generated code. Can be any CodeDOM language installed on the machine. This value defaults to C#.
  • Namespace: The namespace of the generated code
  • ConfigClassName: The name of the static class used to access the configuration information. This defaults to the Pascal-cased name of the root XML element. For example, if the root element is <service-details>, then the configuration class name defaults to ServiceDetails.
  • ConfigPropertyName: The name of the static property in the configuration class that represents the instance of the ConfigurationSection class. This defaults to "Config".

Last edited Jul 11, 2011 at 8:24 AM by jeevanjj, version 11


No comments yet.