鉴于此 XML 文件:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<mySection>
<NetworkShare folder="\\myFolder\v1" />
</mySection>
</configuration>
以下代码不起作用(给出空白输出):
var navigator = XElement.Load(ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).FilePath).CreateNavigator();
var q = navigator.Select("/configuration/mySection/NetworkShare");
if (q.Count == 1) // q.Count is 0
{
q.MoveNext();
Console.WriteLine(q.Current.GetAttribute("folder", navigator.Prefix));
}
但是这段代码有效(正确打印出文件夹):
var navigator = XDocument.Load(ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).FilePath).CreateNavigator();
var q = navigator.Select("/configuration/mySection/NetworkShare");
if (q.Count == 1) // q.Count is 1
{
q.MoveNext();
Console.WriteLine(q.Current.GetAttribute("folder", navigator.Prefix));
}
这是.net中的错误吗?