1
votes

For ou application we use a XML File which is generated during build process (at the moment with NAnt). In the XML file is for each application Module a section. Now I want to switch from NAnt to MSBuild.

I want to append for each Module the Module descriptions to our XML File. The XML file should have the following structure

<?xml version="1.0" encoding="utf-8"?>
<FactoryConfig xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Modules>
    <ModuleConfig>
      <ModuleName>Module1</ModuleName>
      <Description>Module1 Description</Description>
      <AssemblyName>Assembly.Module1</AssemblyName>
      <ModuleClass>Module1.class</ModuleClass>
      <Enabled>true</Enabled>
    </ModuleConfig>
    <ModuleConfig>
      <ModuleName>Module2</ModuleName>
      <Description>Module2 Description</Description>
      <AssemblyName>Assembly.Module2</AssemblyName>
      <ModuleClass>Module2.class</ModuleClass>
      <Enabled>true</Enabled>
    </ModuleConfig>
  </Modules>
</FactoryConfig>

Here my MSBuild Code

<XmlPeek XmlInputPath="$(WebDir)FactoryConfig.xml" Query="/FactoryConfig/Modules">
  <Output TaskParameter="Result" ItemName="Peeked" />
</XmlPeek>

<Message Text="Subtree: @(Peeked)" />

<XmlPoke XmlInputPath="$(WebDir)FactoryConfig.xml" Query="/FactoryConfig/Modules" Value="@(Peeked)
        &lt;ModuleConfig&gt;
        &lt;ModuleName&gt;Test&lt;/ModuleName&gt;
        &lt;Description&gt;Test&lt;/Description&gt;
        &lt;AssemblyName&gt;Test&lt;/AssemblyName&gt;
        &lt;ModuleClass&gt;Test&lt;/ModuleClass&gt;
        &lt;Enabled&gt;true&lt;/Enabled&gt;
        &lt;/ModuleConfig&gt;">
</XmlPoke>

I can add one section to the xml file. But I don't know how to read the existing tree as string and merge it with a new one and add it to the file.

If a Module is already in there it should be replaced.

1
Might be easier to write some C# code to do it, then invoke that from msbuildstijn
Yeah you're probably at the point where you want to start looking into perhaps writing a Custom MSBuild Task msdn.microsoft.com/en-us/library/t9883dzc.aspxaolszowka

1 Answers

1
votes

I suppose MSBuild.ExtensionPack.Xml.XmlFile is what exactly you need for. Play with AddElement/UpdateElement actions.