zoukankan      html  css  js  c++  java
  • winform利用读取xml获取webconfig

    一、利用读取xml获取web.config中的数据库连接

    参考:传送门

     #region 利用读取xml获取webconfig
    private static String GetXML()
    {
    //待修改
    String f = System.Windows.Forms.Application.StartupPath;
    f = f.Replace("调试", "CommonMethod\\Web.config");//对路径进行处理
    XmlDocument doc = new XmlDocument();
    doc.Load(f);
    XmlNodeList add = doc.SelectNodes("//appSettings/add");
    String DBString = "";
    foreach (XmlNode node in add)
    {
    if (node.Attributes["key"].Value.Equals("strConn"))
    {
    DBString = node.Attributes["value"].Value;
    break;
    }

    }
    return DBString;
    }



    二、获取webconfig的路径

    System.IO.Directory.GetCurrentDirectory() 
    这个方法,会随着你的当前系统路径的改变而改变.比如你打开一个openFileDialog那么,再次获得路径就不对了。
    string m_PathStr = Application.ExecutablePath;
    是能获得全部,包括exe文件名的一串东西比如c:\abc\abc.exe
    Application.StartupPath
    能够获得当前的运行路径,不包括执行文件名
    比如c:\abc\(又没有那个“\|”不记得了)
    自己分析你要那个吧

    参考:传送门
    三、利用xml读写web.config

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Xml;
    using System.IO;
    using System.Drawing.Imaging;
    using System.Data.SqlClient;
    using System.Windows.Forms.Integration;

    namespace WindowsFormsApplication1
    {
    public partial class Form1 : Form
    {
    public Form1()
    {
    InitializeComponent();
    }

    String filePath = "";
    private void openFile_Click(object sender, EventArgs e)
    {
    DataTable dataTable1 = new DataTable();
    System.Data.DataRow dr;
    dataTable1.Columns.Add(new System.Data.DataColumn("key", typeof(System.String)));
    dataTable1.Columns.Add(new System.Data.DataColumn("value", typeof(System.String)));
    if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
    {
    filePath = openFileDialog1.FileName;
    if (!filePath.ToLower().EndsWith("web.config"))
    {
    MessageBox.Show("不是web.config");
    return;
    }
    XmlDocument doc = new XmlDocument();
    doc.Load(filePath);
    XmlNodeList add = doc.SelectNodes("//appSettings/add");
    foreach (XmlNode node in add)
    {
    dr = dataTable1.NewRow();
    dr[0] = node.Attributes["key"].Value;
    dr[1] = node.Attributes["value"].Value;
    dataTable1.Rows.Add(dr);
    }
    this.dataGridView1.DataSource = dataTable1;
    }
    }

    private void saveButton_Click(object sender, EventArgs e)
    {
    XmlDocument doc = new XmlDocument();
    doc.Load(filePath);
    XmlNode appSettings = doc.SelectSingleNode("//appSettings");
    appSettings.RemoveAll();
    XmlNodeList add = doc.SelectNodes("//appSettings/add");
    for (int i = 0; i < dataGridView1.Rows.Count; i++)
    {
    if (dataGridView1.Rows[i].Cells[1].Value != null && dataGridView1.Rows[i].Cells[0].Value != null)
    {
    String key = dataGridView1.Rows[i].Cells[0].Value.ToString();
    String value = dataGridView1.Rows[i].Cells[1].Value.ToString();
    if (key.Equals("") || value.Equals("")) return;

    XmlNode newNode = doc.CreateElement("add");
    XmlAttribute attKey = doc.CreateAttribute("key");
    attKey.Value = key;
    newNode.Attributes.Append(attKey);
    XmlAttribute attVaue = doc.CreateAttribute("value");
    attVaue.Value = value;
    newNode.Attributes.Append(attVaue);
    appSettings.AppendChild(newNode);

    }
    doc.Save(filePath);
    }
    }

    }
    }



  • 相关阅读:
    VS2013
    有了门面,程序会更加体面!- pos软件基于三层架构 -09
    无熟人难办事?- 闲聊设计模式-迪米特法则
    三层架构,分层开发
    Filezilla 错误
    归档和压缩
    在Linux系统下用dd命令制作ISO镜像U盘启动盘
    脚本语言
    node.js知识点提取
    npm cnpm
  • 原文地址:https://www.cnblogs.com/0banana0/p/2335727.html
Copyright © 2011-2022 走看看