Asp.net custom Config File Read

Source: Internet
Author: User

This project used to read the custom config file, so I wrote this example as my technical accumulation.

 

I created a URL. config file under the root directory of the website.

The content is as follows:

<? XML version = "1.0" encoding = "UTF-8" ?>
< URL >
< Add Key = "Eva" URL = "Http: // localhost: 7572/evaservice. asmx" />
< Add Key = "Energyindex" URL = "Http: // localhost: 7572/EAI/energyindexservice. asmx" />
</ URL >

 

Then I needProgramTo read the. config file. Previously, all the files were read in XML, txt, and other files have not been tried. In fact, it is quite simple.

 

Public Static String Geturl ( String Key)
{
String Url = "" ;
Xmltextreader reader = New Xmltextreader (system. Web. httpcontext. Current. server. mappath ( " ~ \ URL. config " ));
Xmldocument Doc = New Xmldocument ();
Doc. Load (Reader );

Foreach (Xmlnode Node In Doc. documentelement. childnodes)
{
If (Node. attributes [ " Key " ]. Value = key)
{
Url = node. attributes [ " URL " ]. Value;
Break ;
}
}
Return URL;

}

 

The aboveCode, I put it in the method of a class library, it should be noted that the relevant namespace should be referenced

Call:

 

Xmlstream. geturl ( " Energyindex " ); // Get http: // Localhost: 7572/EAI/energyindexservice. asmx

 

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.