Search code examples
xmlmsbuildmsbuildextensionpack

Set property in a separate file


I have an XML file that is storing a GUID as a property and I would like to set this property value from another XML file. I noticed Msbuild might be able to do it. I figured out how to read this property, but I'm having trouble actually setting a value and saving the file.

Here is the code in my XML file that stores the GUID as a property (file name is GUID.properties):

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">  
    <PropertyGroup>    
        <GUIDProperty>NULL</GUIDProperty>
    </PropertyGroup>
</Project>

Here is the code in my XML where I am trying to set the property for the GUID:

<!-- Create GUID for Installation -->
    <MSBuild.ExtensionPack.Framework.Guid TaskAction="Create">
    <Output TaskParameter="FormattedGuidString" PropertyName="GuidString" />
    </MSBuild.ExtensionPack.Framework.Guid>    

<!-- Set GUIDProperty in GUID.property -->
    <MSBuild.ExtensionPack.Xml.XmlFile TaskAction="UpdateElement" File="$(MSBuildProjectDirectory)\GUID.properties" XPath="GUIDProperty" InnerText="$(GuidString)"/>

So, all I'm looking to do is set that GUID property in GUID.properties. If there is an easier way without Msbuild, I'm all for that, but to me, it looked like I would probably need Msbuildextensionpack. I'm not extremely familiar with XML and Msbuild which is why I'm asking here.


Solution

  • I don't actually think you need msbuildextensionpack. You can do it just by using msbuild.

    Example:

    <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
    <Target Name="UpdateGuid">
        <PropertyGroup>
            <_xmlFile>guid.properties</_xmlFile>
            <_newGuid>$([System.Guid]::NewGuid())</_newGuid>
        </PropertyGroup>
        <XmlPoke Namespaces="&lt;Namespace Prefix='msb' Uri='http://schemas.microsoft.com/developer/msbuild/2003'/&gt;"
                 XmlInputPath="$(_xmlFile)" 
                 Query="/msb:Project/msb:PropertyGroup/msb:GUIDProperty"
                 Value="$(_newGuid)" />
    </Target>
    </Project>