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