. net--using. Net built-in handlers to handle their own defined nodes demo

Source: Internet
Author: User


In. Net. Because of the different nodes, the corresponding class to deal with it,. NET inside for convenience. We have built in some classes for our use. Allows us to read the configuration file. You do not have to define your own classes to handle your own defined nodes.


Below we have written a configuration file:


<?xml version= "1.0" encoding= "Utf-8"?><configuration>  <configSections>    <!-- Use Ignoresection to process your own defined nodes-    <!--<section name= "mailserveraddress" type= " System.Configuration.IgnoreSection, System.Configuration, version=4.0.0.0, Culture=neutral, publickeytoken= B03F5F7F11D50A3A "allowlocation=" false "restartonexternalchanges=" true "/>-->    <section name=" Mailserveraddress "type=" System.Configuration.SingleTagSectionHandler "/>        <!--Note that Specifies that the handler's configuration file is written in front of its own definition profile-  </configSections>      <mailserveraddress address= " Mail.tracefact.net "Username=" LHC "password=" 124324 "/> </configuration>



The node name is: Mailserveraddress. There are three attributes that define the SingleTagSectionHandler in the section to handle this node.


namespace self-defined node and built-in handler {    class program    {        static void Main (string[] args)        {            Examplesingletagsectionhandler ();        }        private static void Examplesingletagsectionhandler () {            //singletagsectionhandler returns all the properties            of a node as Hashtable Hashtable mailserver = (Hashtable) configurationmanager.getsection ("mailserveraddress");// Calling GetSection returns a hashtable            string address = mailserver["Address". ToString ();            String username = mailserver["username"]. ToString ();            String PassWord = mailserver["PassWord"]. ToString ();            Console.WriteLine (address+ "----" +username+ "------" +password);}}}    


After the configuration file is written, the GetSection Hashtable can be called to read the attribute values of the node in the form of Key--value.


In. NET, in addition to this type in the example above, we can also use other built-in type to handle our own defined nodes.













. net--using. Net built-in handlers to handle their own defined nodes demo

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.