msbuildmsbuildextensionpack

MSBuild.ExtensionPack.Xml.XmlFile TaskAction="ReadAttribute" Failing


I cannot read the value attribute of <add name="ReleaseVersion" value="4"/> in the app.config below. I am at a complete loss. I suspect the XPath value or a Key value.

Target

  <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>

App.Config

  <?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>

Update

I reviewed the code for XmlFile.ReadAttribute at http://msbuildextensionpack.codeplex.com/SourceControl/changeset/view/83099#1714660 and it makes the call SelectSingleNode using the namespace syntax. That could be the problem.

    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;
        }
    }

Solution

  • Your sample code is almost correct; it just needs a slight change to the usage of the XmlFile task. The call to XmlFile ReadAttribute should declare Value as the task's output. To do this, add the Output element to the task declaration, set to the TaskParameter value to "Value" and set the PropertyName value to "ReleaseVersion", similar to the following:

    <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>
    

    After making the change, the attribute value should be found/read by the task, assuming your ToolsVersion is set to the version needed by your particular implementation of MSBuild Extension Pack