简体   繁体   English

如何在winforms中将外部app.config读取为xml文件?

[英]how to read external app.config as xml file in winforms?

How to read app.config file as normal xml and read the connectionString Key/value 如何将app.config文件作为普通xml读取并读取connectionString键/值

from configuration ->connectionStrings node 来自configuration - > connectionStrings节点

<?xml version="1.0"?>
  <configuration>
    <connectionStrings>
       <add name="appConnStr" connectionString="Data Source=DEV6-PC;Initial Catalog=ireg.est;Persist Security Info=True;User ID=sa;Password=sa@123" providerName=".NET Framework Data Provider for SQL Server"/>
    </connectionStrings>
  <configuration>
var element = XDocument.Load("filepath")
                       .Descendants("connectionStrings")
                       .FirstOrDefault();
var connStrings = new Dictionary<string,string>();
if(element != null)
{
   foreach(var item in element.Elements("add"))
   {
      var name = (string)item.Attribute("name");
      var connString = (string)item.Attribute("connectionString");
      connStrings.Add(name,connString);
  }
}

try this: here im reading the file as an xml document and retrieving the connection string attribute. 试试这个:这里我将文件作为xml文档读取并检索连接字符串属性。

string connString=null;
XmlDocument xmldoc = new XmlDocument();
xmldoc.load("yourconfigfielpath"); // add your file path here.
XmlNodeList nodeList = xmlDoc.SelectNodes("/xml/configuration/connectionstrings");
foreach (XmlNode node in nodeList)
{
         connString=node["add"].GetAttribute("connectionString");
}

hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM