13th Chapter. NET application Configuration

Source: Internet
Author: User

usingSystem;usingSystem.Collections.Generic;usingsystem.linq;usingsystem.text;usingsystem.collections;usingsystem.configuration;usingsystem.xml;namespacecustomconfig{ public classMailserverconfighandler:iconfigurationsectionhandler { public ObjectCreate (ObjectParentObjectconfigcontext, System.Xml.XmlNode Section) {mailserverconfig Config=NewMailserverconfig (); Config. Provider= Section. attributes["provider"] ==NULL?"": Section. attributes["provider"].            Value; foreach(XmlNode Childinchsection . ChildNodes) {mailserver Server=Newmailserver (); Server. Client= Child. attributes["Client"] ==NULL?"": Child. attributes["Client"].                Value; foreach(XmlNode grandchildinchchild . ChildNodes) {server.                Servernode.add (grandchild.name, grandchild.innertext); } config.            ADD (server); }            returnconfig; }    }     public classMailServer {PrivateHashtable servernode;  publicmailserver () {servernode=NewHashtable (); }         publicHashtable Servernode {Get{returnservernode;} }         public stringClient {Get;Set; }  public stringAddress {Get{returnservernode["Address"] as string; } }         public stringUserName {Get{returnservernode["username"] as string; } }         public stringPassword {Get{returnservernode["Password"] as string; } }    }     public classMailserverconfig:list<mailserver>    {         public stringProvider {Get;Set; } }}
View Code
<?xml version="1.0"encoding="Utf-8"?><configuration> <configSections> <section name="Mailservergroup"Type="Customconfig.mailserverconfighandler,customconfig"/> </configSections> <mailservergroup provider="www.edong.com"> <mailserver client="forum.tracefact.net"> <address>addr1</address> <userName>user1</userName> <password>pwd1</p assword> </mailServer> <mailserver client="blog.tracefact.com"> <address>addr2</address> <userName>user2</userName> <password>pwd2</p Assword> </mailServer> </mailServerGroup></configuration>
View Code
usingSystem;usingSystem.Collections.Generic;usingsystem.linq;usingsystem.text;usingcustomconfig;usingsystem.configuration;namespaceconsoleapplication1{classprogram {Static voidMain (string[] Args)        {example (); }        Private Static voidexample () {mailserverconfig servergroup= (mailserverconfig) Configurationmanager.getsection ("Mailservergroup"); Console.WriteLine ("provider:\t"+servergroup.provider); foreach(mailserver ConfiginchServergroup) {console.writeline ("-------------------------------"); Console.WriteLine ("client:\t"+Config.                Client); Console.WriteLine ("address:\t"+Config.                Address); Console.WriteLine ("username:\t"+Config.                UserName); Console.WriteLine ("password:\t"+Config.            Password);        } Console.readkey (); }    }}
View Code

13th Chapter. NET application Configuration

Related Article

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.