Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a XML file with MSBuild?

Tags:

xml

msbuild

I'd like to create a XML file within a MSBuild task.

I have a list of files:

<CreateItem Include="$(TestsAssembliesOutputDir)\Emidee.*.Tests.dll">
  <Output ItemName="TestsAssemblies" TaskParameter="Include" />
</CreateItem>

I'd like to create a XML which would look like:

<?xml version="1.0" encoding="utf-8"?>
<xunit>
  <assemblies>
    <assembly filename="PATH OF FILE #1" shadow-copy="true" />
    <assembly filename="PATH OF FILE #2" shadow-copy="true" />
  </assemblies>
</xunit>

How can I achieve that?

Thanks in advance

Mike

like image 906
Emidee Avatar asked Apr 14 '11 15:04

Emidee


People also ask

What is MSBuild XML?

The Microsoft Build Engine is a platform for building applications. This engine, which is also known as MSBuild, provides an XML schema for a project file that controls how the build platform processes and builds software.

How do I create a project using MSBuild command line?

To build a specific target of a specific project in a solution. At the command line, type MSBuild.exe <SolutionName>. sln , where <SolutionName> corresponds to the file name of the solution that contains the target that you want to execute.


1 Answers

Quick and dirty...

<Target Name="CreateXml">
  <ItemGroup>
    <TestAssembly Include="$(TestsAssembliesOutputDir)\Emidee.*.Tests.dll" />
    <Line Include="line01"><Text>&lt;xunit&gt;</Text></Line>
    <Line Include="line02"><Text>&lt;assemblies&gt;</Text></Line>
    <Line Include="line03"><Text>&lt;assembly filename=&quot;%(TestAssembly.Identity)&quot; shadow-copy=&quot;true&quot; /&gt;</Text></Line>
    <Line Include="line04"><Text>&lt;/assemblies&gt;</Text></Line>
    <Line Include="line05"><Text>&lt;/xunit&gt;</Text></Line>
    <LineText Include="%(Line.Text)" />
  </ItemGroup>
  <WriteLinesToFile
     File="out.xml"
     Lines="@(LineText)"
     Overwrite="true"
     />
</Target>

Left as an exercise for you

  • The initial < ? xml line
  • Indentation (hint use CDATA inside <`Text>)

You could also use the following in WriteLinesToFile and omit the synthesized @(LineText)

    Lines="@(Line->'%(Text)')"
like image 186
Brian Kretzler Avatar answered Oct 16 '22 07:10

Brian Kretzler