c# - 使用 LINQ 更新 app.config

标签 c# xml linq linq-to-xml app-config

我有一个 app.config 文件。

我想检索和编辑一个属性:

<appSettings>
    <add key="ProcessorInstances" value="2" />
</appSettings>

我正在使用这个查询:

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; 

但现在我面临这样的因素:

<app>
    <file value="../../../logs/Intel.Service.log" />
</app>

和:

<baseAddresses>
    <add baseAddress="http://Machinename:portnumber/servicename/" />
</baseAddresses>

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

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

最佳答案

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

例如

string baseAddress = ConfigurationManager.AppSettings["baseAddress"].ToString();

关于c# - 使用 LINQ 更新 app.config,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11397711/

相关文章:

linq - 动态 LINQ 喜欢

c# - 多次调用 SuppressFinalize

c# - RhinoMocks stub 具有简化的实现,尝试方法模式

python - 如何将 python 变量编码为 URL 和网络服务器响应?

xml - 如何使用 Groovy MarkupBuilder 插入 xml 注释?

c# - 使用参数中的字段名称对 IList 进行排序

没有变量/参数声明的 C# lambda 表达式?

c# - .NET BitmapSource 正在锁定文件

c# - Xamarin 表格操作相机给出空异常

java - 如何在 Java 中解析带有 "Opening and ending tag mismatch"的 XML 文件