我无法在下面的app.config中读取<add name="ReleaseVersion" value="4"/>
的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;
}
}
答案 0 :(得分:6)
您的示例代码几乎是正确的;它只需要稍微改变XmlFile任务的使用。对XmlFile ReadAttribute的调用应声明 Value 作为任务的输出。要执行此操作add the Output element to the task declaration,请将TaskParameter
值设置为“值”,并将PropertyName
值设置为“ReleaseVersion”,类似于以下内容:
<MSBuild.ExtensionPack.Xml.XmlFile
TaskAction="ReadAttribute"
File="[example-path-to-app-config]"
XPath="/configuration/system.diagnostics/switches/add[@name='ReleaseVersion']/@value">
<Output TaskParameter="Value" PropertyName="ReleaseVersion" />
<MSBuild.ExtensionPack.Xml.XmlFile>
进行更改后,任务应找到/读取属性值,假设您的ToolsVersion设置为特定MSBuild扩展包实现所需的版本