Click here to Skip to main content
15,861,125 members
Articles / Programming Languages / XML
Article

Reading and Writing Configuration Application Block: Enterprise Library 1.0

Rate me:
Please Sign up or sign in to vote.
3.75/5 (9 votes)
27 Apr 2005CPOL3 min read 77.9K   649   32   8
Reading and writing Configuration Application Block.

Introduction

The Enterprise Library is altogether a new concept in the .NET Framework. I found very few resource on this in the Internet. Here I show how to implement the reading and writing of configuration files of the Enterprise Library coupled with your web application. On exploring the strength of the application, I thought of presenting the new concepts of the Enterprise Library for application development. So here I am presenting one of the blocks of Enterprise Library. Before going to this sample, please go through the actual concepts written by Microsoft MSDN.

Key Scenario:

For running your application, you require to install Enterprise Library 1.0. One can download Enterprise Library 1.0 here.

Dependencies

  • Microsoft.Practices.EnterpriseLibrary.Configuration.dll

Step By Step Implementation

Here we go with actual Configuration Application block where one can learn to read and write the configuration files.

  1. Create one ASP.NET Web Application project named Configuration Application Block, from Microsoft Visual Studio..
  2. Under this project, create one web form named RXConfiguration.aspx.
  3. This is an important step. If Enterprise Library is installed in your system, go to Program menu, click on ‘Microsoft Patterns and Practices’, there you will find ‘Enterprise Library’, under it, open ‘Enterprise Library Configuration’.
  4. Once Enterprise Library Configuration is open, click on File menu, select Open Application. Now you will see the file browser window. Browse your application under inetpub/wwwroot/Configuration Application Block and select Web.config file and click OK.
  5. See the image below. You will find that under Enterprise Library Configuration, there appears an Application menu. Now right click on Application, select New, and under it select Configuration Application Block.

    Sample Image

  6. You will find Configuration Application Block appears below the node Application. Next step is to right click on Configuration Application Block and select Configuration Section.

    Important note: Change the name of Configuration Section to FormatSection.

    Sample Image

  7. Right click on Configuration Section (FormatSection), select New, under which appears XML File Storage Provider. On the right side, one can find the General section of XML File Storage Provider. Give some filename as formatsetting.config.

    Sample Image

  8. Again, right click on Configuration Section, select New, under which appears XML Serializer Transformer.
  9. Now save the file.
  10. On doing so, go to your ASP.NET web application. In Solution Explorer, click on Show All Files icon. You will find that formatseting.config file appears on project files.
  11. Add References. Click on Browse, path Program Files/Microsoft Enterprise Library/bin. Add References of Microsoft.Practices.EnterpriseLibrary.Configuration.dll.

    Sample Image

  12. Now do the code behind coding in RXConfiguration.aspx.cs.

See the user interface of this example:

Sample Image

Include the namespace: using Microsoft.Practices.EnterpriseLibrary.Configuration.

Before this, create one class file named ClsFormatSetting.cs where one can specify the properties and methods required for reading and writing Confiq files.

C#
public class FormatSettingData
{
    private string  firstName;
    private string  lastName;
    private string  designation;
    private string  filePath;
    private string  name;
    private float   size;
    private int     style;
    public FormatSettingData()
    {          
    }

    public string Name 
    {
        get{ return name; }
        set{ name = value; }
    } 
    public string FirstName 
    {
        get{ return firstName; }
        set{ firstName = value; }
    } 
    public string LastName 
    {
        get{ return lastName; }
        set{ lastName = value; }
    } 
    public string Designation 
    {
        get{ return designation; }
        set{ designation = value; }
    } 
    public string FilePath 
    {
        get{ return filePath; }
        set{ filePath= value; }
    } 
    public float Size 
    {
        get{ return size; }
        set{ size = value; }
    } 

    public int Style 
    {
        get{ return style; }
        set{ style = value; }
    } 

    public override string ToString() 
    {
        StringBuilder sb = new StringBuilder();
        sb.AppendFormat("FirstName = {0}; LastName = {1};" + 
                   " Designation = {2}; FilePath={3}", 
                   FirstName.ToString(), LastName.ToString(), 
                   Designation.ToString(),FilePath.ToString());
        return sb.ToString();
    }
}

The above code specifies person details, file path, format font type. I would have made it more object oriented by defining different classes for them. In order to make things look simpler, I did this.

Now time to write some code in RXConfiguration.aspx.cs.

C#
private FormatSettingData objFormatData=new FormatSettingData();
public void btnWriteXml_Click(object sender, System.EventArgs e)
{
    // This write into Configuration file created by Enterprise Library Console
    //Write into File named formatsetting.config
    btnReadXml.Enabled=true;
    btnFormatText.Enabled=true;

    //Data required to be written into Congig file

    objFormatData = new FormatSettingData();
    objFormatData.FirstName="Santosh";
    objFormatData.LastName="Poojari";
    objFormatData.Designation="Programmer";
    objFormatData.FilePath="http://www.codeprojects.com";
    objFormatData.Name = "Tahoma";
    objFormatData.Size = 14;
    objFormatData.Style = 2;

    // Write the new configuration data to the XML file
    //***Important In Enterprise Library Change Configuration Section into
    //FormatSection
    ConfigurationManager.WriteConfiguration("FormatSection", objFormatData);
    btnWriteXml.Enabled=false;
}

Read configuration data from formatsetting.config file:

C#
//On click of Read button data from Config file get populated into 
//Text Area Box as shown in figure above.
public void btnReadXml_Click(object sender, System.EventArgs e)
{
    // Read configuration data from formatsetting.config file 
    objFormatData =
      ConfigurationManager.GetConfiguration("FormatSection") as FormatSettingData; 
    txtReader.Text=objFormatData.ToString();
}

Now we can format the text in the text area box with the code below. See that we have specified the format of text information in the Config file.

C#
public void btnFormatText_Click(object sender, System.EventArgs e)
{
    //Do the formatting of text from font settings 
    //defined in formatsetting.config file
    objFormatData =
        ConfigurationManager.GetConfiguration("FormatSection") 
        as FormatSettingData;
    txtReader.Font.Name=objFormatData.Name;
    txtReader.Font.Size=(FontUnit)objFormatData.Size;
}

You can see the format of XML written in the formatsetting.config file.

XML
<?xml version="1.0" encoding="utf-8"?>
< FormatSection>
  < xmlSerializerSection type=
    "Configuration_Application_Block.FormatSettingData, 
     Configuration Application Block, Version=1.0.1937.19238, 
     Culture=neutral, PublicKeyToken=null">
    < FormatSettingData xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      < Name>Tahoma</Name>
      < FirstName>Santosh < /FirstName>
      < LastName>Poojari < /LastName>
      < Designation>Programmer < /Designation>
      < FilePath>http://www.codeprojects.com</FilePath>
      < Size>14</Size >
      < Style>2</Style >
    < /FormatSettingData >
  < /xmlSerializerSection >
</FormatSection >

That's all...

References

Conclusion

The basic idea is to give a jumpstart to the newer technologies on the block. Hope I have reached to the expectation of the learner. Criticism, suggestions, any information are most welcomed.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead
Australia Australia
Whatsup-->Exploring--> MVC/HTML5/Javascript & Virtualization.......!
www.santoshpoojari.blogspot.com

Comments and Discussions

 
GeneralApplication block data source modification Pin
Delphig27-Feb-06 19:33
Delphig27-Feb-06 19:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.