我想从另一个项目的外部app.config(appSettings)加载一些配置,加载的值必须保存在我的一些属性中。这里(参见代码中的注释)是我想做的事:使用Linq而不是foreach
XmlDocument xmlDoc = MyXmlDocument;
if (xmlDoc != null)
{
XmlNodeList appSettings = xmlDoc.SelectNodes("/configuration/appSettings/add");
if (appSettings != null && appSettings.Count > 0)
{
foreach (XmlNode node in appSettings)
{
XmlAttribute keyAttr = node.Attributes["key"];
if (keyAttr != null)
{
if (keyAttr.Value == "MyProperty1NameInConfigFile") MyProperty1 = node.Attributes["value"].Value;
// ....
}
}
// Instead of using foreach loop, I want to use Linq like this:
var node = get me the node that has the keyAttribute.Value == "MyProperty1NameInConfigFile"
MyProperty1 = node.Attributes["value"].Value;
// If I got this, then I can later use another method for each property like this:
SaveConfigToMyProperty(ref MyProperty1, "MyProperty1NameInConfigFile");
SaveConfigToMyProperty(ref MyProperty2, "MyProperty2NameInConfigFile");
// ...
}
}
为什么您需要使用XML读取器来使用app.config? –
,因为我正在使用另一个项目的app.config。我想从外部app.config读取设置。 – Stacked
可能希望为这个问题添加一些小技巧,这样人们(比如我)就不会威胁到你。 ;) –