不知道为什么写了这个,想得好复杂,写来好简单。
ELConfig.cs
- using System;
- using System.Data;
- using System.Configuration;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.HtmlControls;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Xml;
- /// <summary>
- ///ELConfig 的摘要说明
- /// </summary>
- public class ELConfig
- {
- public ELConfig(Environment m)
- {
- //
- //TODO: 在此处添加构造函数逻辑
- //
- this.en = m;
- }
- readonly string configroot = System.Web.HttpContext.Current.Server.MapPath("~/app_data/global_config.xml");
- XmlDocument xmldoc = new XmlDocument();
- public enum Environment {sandbox,taobao };
- Environment en;
- public string appkey
- {
- get{ return GetXmlInnerText("appkey");}
- }
- public string appsecret
- {
- get { return GetXmlInnerText("appsecret"); }
- }
- public string sessionkey
- {
- get { return GetXmlInnerText("sessionkey"); }
- }
- public string serverurl
- {
- get { return GetXmlInnerText("serverurl"); }
- }
- string GetXmlInnerText(string name)
- {
- xmldoc.Load(configroot);
- string result="";
- switch (en)
- {
- case Environment.sandbox:
- result=xmldoc.SelectSingleNode("root").ChildNodes[0][name].InnerText;
- break;
- case Environment.taobao:
- result=xmldoc.SelectSingleNode("root").ChildNodes[1][name].InnerText;
- break;
- default:
- result = "";
- break;
- }
- return result;
- }
- }
global_config.xml
- <?xml version="1.0" encoding="utf-8" ?>
- <root>
- <sandbox>
- <serverurl>http://gw.api.tbsandbox.com/router/rest</serverurl>
- <appkey>test</appkey>
- <appsecret>test</appsecret>
- <sessionkey></sessionkey>
- </sandbox>
- <taobao>
- <serverurl>http://gw.api.taobao.com/router/rest</serverurl>
- <appkey>yefor</appkey>
- <appsecret>yefor</appsecret>
- <sessionkey></sessionkey>
- </taobao>
- </root>