使用LINQ更新app.config

use*_*658 1 c# xml linq app-config linq-to-xml

我有一个app.config文件.

我想要检索和编辑属性:

<appSettings>
    <add key="ProcessorInstances" value="2" />
</appSettings>
Run Code Online (Sandbox Code Playgroud)

我正在使用此查询:

var list5 = from appNode in doc2.Descendants("appSettings").Elements() 
            where appNode.Attribute("key").Value == "ProcessorInstances" 
            select appNode;
var element5 = list5.FirstOrDefault();
string five = element5.Attribute("value").Value; 
Run Code Online (Sandbox Code Playgroud)

但现在我面临的问题包括:

<app>
    <file value="../../../logs/Intel.Service.log" />
</app>
Run Code Online (Sandbox Code Playgroud)

和:

<baseAddresses>
    <add baseAddress="http://Machinename:portnumber/servicename/" />
</baseAddresses>
Run Code Online (Sandbox Code Playgroud)

这些元素没有任何键属性.

如何编写LINQ查询来编辑它们?

Mar*_*nce 7

将app.Config文件解析为xml并使用Linq不是从App.Config获取设置的方法.而是使用System.Configuration.ddl(在项目中添加对此文件的引用).

例如

string baseAddress = ConfigurationManager.AppSettings["baseAddress"].ToString();
Run Code Online (Sandbox Code Playgroud)