3

我无法读取<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;
        }
    }
4

1 回答 1

6

您的示例代码几乎是正确的;它只需要对 XmlFile 任务的用法稍作改动。对 XmlFile ReadAttribute 的调用应将Value声明为任务的输出。为此,请将 Output 元素添加到任务声明中,将TaskParameter值设置为“Value”并将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 扩展包实现所需的版本

于 2013-04-02T03:46:24.483 回答