数据是
  <appSettings>
    <add key="WebTitle" value="标题"></add>
    <add key="Key" value="关键字"></add>
    <add key="Description" value="描述"></add>
    <add key="Url" value="http://WWW.XXX.com"></add>
    <add key="WebSite" value="www.XXX.com"></add>
    <add key="FootStr" value="尾部字段"></add>
    
  </appSettings>我选择的是
XDocument XDoc = XDocument.Load(MyConfig.WebPath + "\\Web.config");但是 之后我怎么匹配key 然后读取value呢???

解决方案 »

  1.   

    直接用System.Configuration.ConfigurationManager.AppSettings[]节点不行么?还要自己写什么?
      

  2.   

    最基本的Web.Config
    <?xml version="1.0" encoding="utf-8" ?>
    <configuration> 
    <appSettings> 
    <add key="y" value="this is Y"/> 
    </appSettings>
    </configuration>读:System.Configuration.ConfigurationManager.AppSettings[“y”];
    加:Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);AppSettingsSection app = config.AppSettings;app.Settings.Add("x", "this is X");config.Save(ConfigurationSaveMode.Modified);
    修改:Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);AppSettingsSection app = config.AppSettings;//app.Settings.Add("x", "this is X");app.Settings["x"].Value = "this is not Y";config.Save(ConfigurationSaveMode.Modified);
    删除:Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);AppSettingsSection app = config.AppSettings;app.Settings.Remove("x");config.Save(ConfigurationSaveMode.Modified);
      

  3.   

    XDocument Doc = XDocument.Load(@"C:\A.xml");
    var Query = from D in Doc.Element("").Elements("appSettings")                        select D;
    foreach (var v in Query)
     {}
      

  4.   

    未能给 XElement 找到 查询模式 select
      

  5.   

    如梦的那个是linq查的,你用VS2008 或者打了3.5的补丁就有了。
      

  6.   

    其实就是xml操作 你可以搜索下 xml操作类 什么的类似的 就好了。
      

  7.   

    不是。。我说的是
    LINQ告诉我对与Elemenet没有select 操作。
      

  8.   

    直接使用如下方法不可以吗?string webTitle = System.Configuration.ConfigurationManager.AppSettings["WebTitle"];如果不行,那就用xml的读取xml的方法,从网上搜索c#读取XML,能搜到很多代码示例
      

  9.   

                var item = from D in Query
                           where D.Attribute("key").Value.ToString() == "WebTitle"
                           select D;
                //XElement XE =(XElement) item;
                //MessageBox.Show(((XElement) item).Attribute("value").Value.ToString());
    纳闷不能直接转换??
      

  10.   

                    string InDoorNum ="";
                    DataSet ds = new DataSet();
                    ds.ReadXml(Application.StartupPath + "\\door.xml");
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        InDoorNum =ds.Tables[0].Rows[i][0].ToString();
                    }希望对你有用
      

  11.   

    我给你的代码,以下是我的系统这样处理的。运行多年了。===================================================
     #region 加载 Web.Config 文件配置
                if (!File.Exists(cfgFile))
                    throw new Exception("找不到配置文件[" + cfgFile + "]2");            DataSet ds = new DataSet();
                ds.ReadXml(cfgFile);
                StreamReader read = new StreamReader(cfgFile);
                string firstline = read.ReadLine();
                string cfg = read.ReadToEnd();
                read.Close();            int start = cfg.ToLower().IndexOf("<appsettings>");
                int end = cfg.ToLower().IndexOf("</appsettings>");            cfg = cfg.Substring(start, end - start + "</appsettings".Length + 1);            cfgFile = "__$AppConfig.cfg";
                StreamWriter write = new StreamWriter(cfgFile);
                write.WriteLine(firstline);
                write.Write(cfg);
                write.Flush();
                write.Close();            DataSet dscfg = new DataSet("cfg");
                try
                {
                    dscfg.ReadXml(cfgFile);
                }
                catch (Exception ex)
                {
                    throw new Exception("加载配置文件[" + cfgFile + "]失败!\n" + ex.Message + "启动失败!");
                }            //   BP.SystemConfig.CS_AppSettings = new System.Collections.Specialized.NameValueCollection();            BP.SystemConfig.CS_DBConnctionDic.Clear();
                DataTable dt = dscfg.Tables["add"];
                foreach (DataRow dr in dt.Rows)
                {
                    string key = dr["key"] as string;
                    if (key == null || key == "")
                        continue;                string value = dr["value"] as string;
                    if (value == null || value == "")
                        continue;                BP.SystemConfig.CS_AppSettings.Add(key, value);
                }
                dscfg.Dispose();                        #endregion
                return true;