System.Configuration.ConfigurationManager.AppSettings[key];这个系统方法读取xml是默认的.config 文件。 请问 如何才能指定某个XML文件然后用这个方法读取呢?

解决方案 »

  1.   


    // Show how to use OpenExeConfiguration(string).
    static void DisplayAppSettingsRawXml()
    {
        // Get the application path.
        string exePath = System.IO.Path.Combine(
            Environment.CurrentDirectory, "ConfigurationManager.exe");    // Get the configuration file.
        System.Configuration.Configuration config =
          ConfigurationManager.OpenExeConfiguration(exePath);    // Get the AppSetins section.
        AppSettingsSection appSettingSection = config.AppSettings;    // Display raw xml.
        Console.WriteLine(appSettingSection.SectionInformation.GetRawXml());
    }
      

  2.   

    使用System.Xml命名空间中所提供的方法,选择性更为大
      

  3.   

    2楼那个还是读取默认的 .config文件。
      

  4.   

    我需要任意xml啊。 不是根据程序获取的xml。 
      

  5.   

    那就把Environment.CurrentDirectory改成你的路径呀!
      

  6.   

    就是。。不过还有点问题。参数key  在哪设置?
      

  7.   

    设置完路径后就可以了,不过要用你设置的那个变量config获取。
    我记得好像是ConfigurationSettings.AppSettings["A"]<add key="A" value="config with A"/> 你试一下。
      

  8.   

    首先,有个问题要澄清,就是ConfigurationManager不能指定配置文件,因为这个静态类设计出来是就是给来读默认的config文件的,app.config或者是web项目里的webconfig。如果可以修改读取的路径,那就是本末倒置了。换个思路,  如果的确是有必要,把自己的配置信息以Xml结构写到新的config后缀的文件中。因为就是个xml文件,所以你知道如何来解析这个xml,对吧,用XmlReader,XElemnt,XDocument等类来解析不是一般的方便。
      

  9.   

    ConfigurationManager只能读取配置文件的,要读取任何XML文件,则得自己写方法
      

  10.   

    自定义读取
    string myWebSiteName = ((NameValueCollection)ConfigurationSettings.GetConfig(""))["name"];