xml文件內容如下:
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<applicationSettings>
<test1.Properties.Settings>
<setting name= 'AppConnection' value=''/>
</test1.Properties.Settings>
</applicationSettings>
</configuration>
取值的代碼:
string configFileName = Application.ExecutablePath + ".config";
MessageBox.Show(configFileName);
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.Load(configFileName);
string configString = @"configuration/applicationSettings/test1.Properties.Settings/setting[@name='AppConnection']/value";
System.Xml.XmlNode configNode = doc.SelectSingleNode(configString);
在最後的 configNode 那裡總是取不到值。大神幫忙看一下問題出在哪兒
你的value是屬性,不是節點,當然選不出,去掉/value
string configString = @"configuration/applicationSettings/test1.Properties.Settings/setting[@name='AppConnection']";
System.Xml.XmlNode configNode = doc.SelectSingleNode(configString);
MessageBox.Show(configNode != null && configNode.Attributes["value"] != null ? configNode.Attributes["value"].Value : "NULL");