. Net config file custom configuration section

Source: Internet
Author: User

For the custom configuration section, I will not talk much about it here. I will not talk much about what I have read from the node and then cache it statically (I have previously written this article ).

Note the node tag and Class Name When deserialization is used (only the serialization tag is added to the class, and each attribute is the default sequence)

Problem: Unable to complete deserialization, no error is reported, but the configuration is not read

Cause: Due to my code habits, the first letter of the class is always in upper case, while the first letter in XML is in lower case.

Note: When deserialization is used, the class name and the tag name must be the same (case sensitive). In my habit, one party must be nonstandard.

Of course, if you add a serialization tag to each attribute and specify the XML element name, this column is not

Sample Code:

1. configuration file example, which is used to configure the Static Page Template

<? XML version = "1.0" encoding = "UTF-8"?>
<Configuration>
<Configsections>
<Section name = "templateconfiguration" type = "journalupdateservice. config. templateconfigurationhandle, windowsapplication1"/>
</Configsections>
<Templateconfiguration>
<Templates>
<Template pagename = "item. aspx">
<Head> E:/projects/cnkiecp/center/web/windowsapplication1/bin/debug/template/item/header.txt <Body> E:/projects/cnkiecp/center/web/windowsapplication1/bin/debug/template/item/body.txt </body>
<Bodyheader> E:/projects/cnkiecp/center/web/windowsapplication1/bin/debug/template/item/bodyheader.txt </bodyheader>
<Bodycontext> E:/projects/cnkiecp/center/web/windowsapplication1/bin/debug/template/item/itembody.txt </bodycontext>
<Bodyfooter> E:/projects/cnkiecp/center/web/windowsapplication1/bin/debug/template/item/bodyfooter.txt </bodyfooter>
</Template>
</Templates>
</Templateconfiguration>
<Deleetask>

</Appsettings>
</Configuration>

2. Configure the node handler function in handle to implement the iconfigurationsectionhandler interface.

Using system;
Using system. configuration;
Using system. xml;
Using system. xml. serialization;
Using system. xml. XPath;

Namespace journalupdateservice. config
{
/// <Summary>
/// Summary of templateconfigurationhandle.
/// </Summary>
Public class templateconfigurationhandle: iconfigurationsectionhandler
{
Public templateconfigurationhandle ()
{
}

Public object create (Object parent, object configcontext, system. xml. xmlnode Section)
{
Xmlserializer SER = new xmlserializer (typeof (templateconfiguration ));

// Return the deserialized object from the Web. config XML
Return Ser. deserialize (New xmlnodereader (section ));
}
}
}

3. configration custom configuration Root Node

Using system;
Using system. collections;
Using system. xml. serialization;

Namespace journalupdateservice. config
{
/// <Summary>
/// Summary of templateconfiguration.
/// </Summary>
/// & Lt; configuration & gt;
/// & Lt; configsections & gt;
/// & Lt; section name = "templateconfiguration"
/// Type = "journalupdateservice. config. templateconfigurationhandle, windowsapplication1"/& gt;
/// & Lt;/configsections & gt;
///
/// & Lt; templateconfiguration & gt;
/// & Lt; templates & gt;
/// & Lt; Template pagename = "" & gt;
/// & Lt; head fileref = ""/& gt;
/// & Lt; body fileref = ""/& gt;
/// & Lt; bodyheader fileref = ""/& gt;
/// & Lt; bodycontext fileref = ""/& gt;
/// & Lt; bodyfooter fileref = ""/& gt;
/// & Lt;/template & gt;
/// & Lt;/templates & gt;
/// & Lt;/templateconfiguration & gt;
/// & Lt;/configuration & gt;
[Serializable ()]
[Xmlroot ("templateconfiguration")]
Public class templateconfiguration
{
Private templatecollection _ templates;
Public templateconfiguration ()
{
//
// Todo: add the constructor logic here
//
}

Public templatecollection templates
{
Get
{
Return _ templates;
}
Set
{
_ Templates = value;
}
}
}
}

4. Collection configuration set to implement the collectionbase Interface

Using system;
Using system. collections;

Namespace journalupdateservice. config
{
/// <Summary>
/// Summary of templatecolection.
/// </Summary>
/// & Lt; Template pagename = "" & gt;
/// & Lt; head fileref = ""/& gt;
/// & Lt; body fileref = ""/& gt;
/// & Lt; bodyheader fileref = ""/& gt;
/// & Lt; bodycontext fileref = ""/& gt;
/// & Lt; bodyfooter fileref = ""/& gt;
/// & Lt;/template & gt;
[Serializable ()]
Public class templatecollection: collectionbase
{
Public templatecollection ()
{
//
// Todo: add the constructor logic here
//
}
Public Virtual void add (template R)
{
This. innerlist. Add (R );
}

Public template this [int Index]
{
Get
{
Return (Template) This. innerlist [Index];
}
Set
{
This. innerlist [Index] = value;
}
}
}
}

5. Configure the object

Using system;
Using system. xml. serialization;

Namespace journalupdateservice. config
{
/// <Summary>
/// Summary of the template.
/// </Summary>
[Serializable ()]
Public class template
{
Private string _ pagename;
Private string _ head;
Private string _ body;
Private string _ bodyheader;
Private string _ bodycontext;
Private string _ bodyfooter;

Public template ()
{
}

[Xmlattribute] // serialize to attribute
Public String pagename
{
Get
{
Return _ pagename;
}
Set
{
_ Pagename = value;
}
}

Public String head
{
Get
{
Return _ head;
}
Set
{
_ Head = value;
}
}

Public String body
{
Get
{
Return _ body;
}
Set
{
_ Body = value;
}
}

Public String bodyheader
{
Get
{
Return _ bodyheader;
}
Set
{
_ Bodyheader = value;
}
}

Public String bodycontext
{
Get
{
Return _ bodycontext;
}
Set
{
_ Bodycontext = value;
}
}

Public String bodyfooter
{
Get
{
Return _ bodyfooter;
}
Set
{
_ Bodyfooter = value;
}
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.