WinForm 读取Web.config 中数据库连接字符串

        private void btnRead_Click(object sender, EventArgs e)
        {
            string str = AppDomain.CurrentDomain.BaseDirectory + "web.config";
            System.Xml.XmlDocument webconfig = new System.Xml.XmlDocument();

            webconfig.Load(str);//web.config的路径
            System.Xml.XmlNode node = webconfig.SelectSingleNode("/configuration/connectionStrings/add[@name='SqlConnectionString']");//要找的节点

            if (node == null)
            {
                MessageBox.Show("没有找到符合要求的节点");
            }
            else
            {
                MessageBox.Show(node.Attributes[1].InnerText);
            }
        }

你可能感兴趣的:(WinFom)