我想从另一个项目的外部 app.config (appSettings) 加载一些配置,加载的值必须保存在我的一些属性中。这里(见代码中的注释)是我想要做的:
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");
// ...
}
}