XML文件格式如下
<appSettings>
<add key="webservice_url" value="http://localhost:1885/Acmeway.Business.Webservices/Service.asmx" />
<\appSettings>
怎么读出http://localhost:1885/Acmeway.Business.Webservices/Service.asmx信息
<appSettings>
<add key="webservice_url" value="http://localhost:1885/Acmeway.Business.Webservices/Service.asmx" />
<\appSettings>
怎么读出http://localhost:1885/Acmeway.Business.Webservices/Service.asmx信息
B要用A的配置文件,能用ConfigruationManager吗
B要用A的配置文件,能用ConfigruationManager吗
lz可以有以下2中方式处理
1.AB程序公用一个WEB.CONFIG文件,如果有2种以上的连接字符串可以用
<appSettings>
<add key="webservice_url1" value="http://localhost:1885/Acmeway.Business.Webservices/Service.asmx" />
<add key="webservice_url2" value="http://localhost:1885/Acmeway.Business.Webservices/Service.asmx" />
<\appSettings>
用 System.Configuration.ConfigurationSettings.AppSettings["webservice_url1"];
System.Configuration.ConfigurationSettings.AppSettings["webservice_url1"];
读出
2.单独读xml文件,也就先用一个XML保存连接字符串信息,然后读该信息,该方法很多
doc.Load(file);
XmlNode node = doc.SelectSingleNode("/appSettings/add");Console.WriteLine(node.Attributes["value"].Value);
XmlNode node = doc.SelectSingleNode("/appSettings/add");
node是NULL的
我的格式
<appSettings>
<add key="webservice_url" value="http://localhost:1885/Acmeway.Business.Webservices/Service.asmx" />
XmlDocument doc = new XmlDocument();
doc.Load(Application.StartupPath + @"\Acmeway.Business.Main.exe.config");
XmlNodeList DocdNodeNameArr = doc.DocumentElement.ChildNodes;//文档节点名称数组
foreach (XmlElement DocXmlElement in DocdNodeNameArr)
{
if (DocXmlElement.Name.ToLower() != "appsettings") continue;//找到名称为 appsettings 的节点 XmlNodeList KeyNameArr = DocXmlElement.ChildNodes;//子节点名称数组
if (KeyNameArr.Count <= 0) continue; string KeyName = "";
string KeyValue = "";
for (int i = 0; i < KeyNameArr.Count; i++)
{
if (!(KeyNameArr[i] is XmlElement)) continue; XmlElement xmlElement = KeyNameArr[i] as XmlElement;
if (xmlElement.Attributes.Count <= 0) continue; KeyName = xmlElement.Attributes["key"].InnerXml;//键值
switch (KeyName)
{
case "webservice_url":
KeyValue = xmlElement.Attributes["value"].Value;
break;
}
}
}