我无法读取<add name="ReleaseVersion" value="4"/>
下面 app.config 中的 value 属性。我完全不知所措。我怀疑 XPath 值或 Key 值。
目标
<Target Name="xxx"
DependsOnTargets="CopyFilesToOutputDirectory" >
<ItemGroup>
<_DestinationAppConfigFile Include="@(AppConfigWithTargetPath->'$(OutDir)%(TargetPath)')" />
</ItemGroup>
<MSBuild.ExtensionPack.Xml.XmlFile TaskAction="ReadAttribute"
File="%(_DestinationAppConfigFile.FullPath)"
XPath="/configuration/system.diagnostics/switches/add[@name='ReleaseVersion']/@value"
Value="$(ReleaseVersion)" />
<Error Condition = " '$(ReleaseVersion)'=='' "
Text="Failed to read attribute." />
<Message Text="ReleaseVersion: $(ReleaseVersion)"
Importance="high" />
</Target>
应用程序配置
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.diagnostics>
<switches>
<!-- ReleaseVersion (Conditional release switch); 0- = PRODUCTION, 1 = MIRROR, 2 = EMERGENCYRELEASE, 3 = USERACCEPTANCETESTING, 4 = QA, 5 = DEVELOPMENT, 6 = DRN DEVELOPMENT -->
<add name="ReleaseVersion" value="4"/>
<!-- Stop (Stops execution to allow for Just-In-Time (JIT) debugging); 0 = CONTINUE EXECUTION, 1 = LAUNCH DEBUGGER -->
<add name="Stop" value="0"/>
</switches>
</system.diagnostics>
</configuration>
更新
我查看了 http://msbuildextensionpack.codeplex.com/SourceControl/changeset/view/83099#1714660 的代码XmlFile.ReadAttribute
,它使用SelectSingleNode
命名空间语法进行调用。这可能是问题所在。
private void ReadAttribute()
{
if (string.IsNullOrEmpty(this.XPath))
{
this.Log.LogError("XPath is Required");
return;
}
this.LogTaskMessage(string.Format(CultureInfo.CurrentUICulture, "Read Attribute: {0}", this.XPath));
XmlNode node = this.xmlFileDoc.SelectSingleNode(this.XPath, this.namespaceManager);
if (node != null && node.NodeType == XmlNodeType.Attribute)
{
this.Value = node.Value;
}
}