views:

1237

answers:

2

Hi

I have a problem with the Xml.ModifyFile task which I do not understand. Can you guys help?

My goal is simply to manipulate an attribute in an xml document.

Im fairly new to the world of xml and especially msbuild hence I how a hard time interpreting the error message i am receiving. It seems to me that my build file is valid so I guess something is wrong in sdc.tasks dll file.

As it can be seen from the build file I have added a target called "ping" for the sake of testing. That target works with the sdc.task Ping without any problems

Can you guys suggest a fix or an alternative solution to the challenge of modifying xml files with msbuild.

An additional question - how do one declare multiple namespaces as argument to the Xml.ModifyFile sdc.task? The explanation of the namespace attribute is as follows: An array of TaskItems specifiying "Prefix" and "Uri" attributes for use with the specified xPath. I have tried to find an explanation or example of the usage of taskitems but unfortunately without any luck.

thanks / derdres

I will list the following below:

  1. build file
  2. the xml file that I try to modify
  3. the error message

1) build file

<Target Name="Go">
    <CallTarget Targets="modify"></CallTarget>
    <!--<CallTarget Targets="ping"></CallTarget>-->
</Target>

<Target Name="modify">
    <Xml.ModifyFile
        Path="C:\Users\Andreas\Desktop\MSBuild\Test_05_april\Test01\bookstore_adv.xml"
        AttributeName="age"
        Force="true"
        XPath="/bookstore/book[@id=2]/@age"
        NewValue="200"
        ShowMatches="Yes"
    >
    </Xml.ModifyFile>

    <Message Text="After modification"></Message>
</Target>

<!--<Target Name="ping">
    <Ping
           Machine="localhost"
           Count="2"
           Interval="1000"
           Timeout="3000"
           BufferSize="1024"
           AllowFragmentation="false"
           TimeToLive="128"
           StopOnSuccess="true"
           LogSuccess="true">
        <Output TaskParameter="FailureCount" PropertyName="FailedPingCount" />
        <Output TaskParameter="RoundTripTime" PropertyName="RoundTripDuration" />
    </Ping>
    <Message Text="FailedPingcount: $(FailedPingCount)"></Message>
    <Message Text="RoundTripDuration: $(RoundTripDuration)"></Message>
</Target>-->

2) xml file

<?xml version="1.0" encoding="utf-8"?>
<!--<bookstore xmlns:hat="www.google.dk/hat" xmlns:briller="www.google.dk/briller">-->
<!--<bookstore xmlns:hat="www.google.dk/hat">-->
<bookstore>
   <book id="1">
        <title>Harry Potter</title>
        <author>Rowling</author>
   </book>
   <book id="2" age="100">
       <title>Lykke Per</title>
       <author>Pontoppidan</author>
   </book>

3) Build Error Message

Build FAILED.

"C:\Users\Andreas\Desktop\MSBuild\Test_05_april\Test01\Deploy.proj" (default target) (1) ->
(modify target) ->
  C:\Users\Andreas\Desktop\MSBuild\Test_05_april\Test01\Deploy.proj(11,9): error : A task error has occured.\r 
C:\Users\Andreas\Desktop\MSBuild\Test_05_april\Test01\Deploy.proj(11,9): error : Message            = Object reference not set to
 an instance of an object.\r
C:\Users\Andreas\Desktop\MSBuild\Test_05_april\Test01\Deploy.proj(11,9): error : Action             = Replace\r
C:\Users\Andreas\Desktop\MSBuild\Test_05_april\Test01\Deploy.proj(11,9): error : Path               = C:\Users\Andreas\Desktop\MS
Build\Test_05_april\Test01\bookstore_advanced.xml\r
C:\Users\Andreas\Desktop\MSBuild\Test_05_april\Test01\Deploy.proj(11,9): error : Namespace          = <null>\r
C:\Users\Andreas\Desktop\MSBuild\Test_05_april\Test01\Deploy.proj(11,9): error : XPath              = /bookstore/book[@id=2]/@age
\r
C:\Users\Andreas\Desktop\MSBuild\Test_05_april\Test01\Deploy.proj(11,9): error : RegularExpression  = <String.Empty>\r
C:\Users\Andreas\Desktop\MSBuild\Test_05_april\Test01\Deploy.proj(11,9): error : NewValue           = 200\r
C:\Users\Andreas\Desktop\MSBuild\Test_05_april\Test01\Deploy.proj(11,9): error : AttributeName      = age\r
C:\Users\Andreas\Desktop\MSBuild\Test_05_april\Test01\Deploy.proj(11,9): error : Force              = True\r
C:\Users\Andreas\Desktop\MSBuild\Test_05_april\Test01\Deploy.proj(11,9): error : TreatNewValueAsXml = False\r
C:\Users\Andreas\Desktop\MSBuild\Test_05_april\Test01\Deploy.proj(11,9): error : ShowMatches        = Yes\r
C:\Users\Andreas\Desktop\MSBuild\Test_05_april\Test01\Deploy.proj(11,9): error : \r
C:\Users\Andreas\Desktop\MSBuild\Test_05_april\Test01\Deploy.proj(11,9): error :    at Microsoft.Sdc.Tasks.Xml.ModifyFile.Interna
lExecute() in c:\projects\codeplex\sdctasks\Solutions\Main\Tasks\Xml\ModifyFile.cs:line 346\r
C:\Users\Andreas\Desktop\MSBuild\Test_05_april\Test01\Deploy.proj(11,9): error :    at Microsoft.Sdc.Tasks.TaskBase.Execute() in
c:\projects\codeplex\sdctasks\Solutions\Main\Tasks\TaskBase.cs:line 66

    0 Warning(s)
    1 Error(s)

Time Elapsed 00:00:00.20
+2  A: 

In your XPath you are searching for the attribute age /bookstore/book[@id=2]/@age but in your task you set the AttributeName to "age". So it is like you want the attribute age of the attribute age.

You just have to change your XPath to /bookstore/book[@id=2] to make it work.

<Target Name="modify">
  <Xml.ModifyFile
    Path="C:\Users\Andreas\Desktop\MSBuild\Test_05_april\Test01\bookstore_adv.xml"
    AttributeName="age"
    Force="true"
    XPath="/bookstore/book[@id=2]"
    NewValue="200"
    ShowMatches="Yes">
  </Xml.ModifyFile>

  <Message Text="After modification"/>
</Target>

How do one declare multiple namespaces as argument to the Xml.ModifyFile sdc.task?

<ItemGroup>
  <Namespace Include="www.google.dk/briller">
    <Prefix>briller</Prefix>
    <Uri>www.google.dk/briller</Uri>
  </Namespace>
  <Namespace Include="www.google.dk/hat">
    <Prefix>hat</Prefix>
    <Uri>www.google.dk/hat</Uri>
  </Namespace>
</ItemGroup>

<Target Name="modify">
  <Xml.ModifyFile
    Path="C:\Users\Andreas\Desktop\MSBuild\Test_05_april\Test01\bookstore_adv.xml"
    AttributeName="age"
    Force="true"
    XPath="/bookstore/book[@id=2]"
    NewValue="200"
    ShowMatches="Yes"
    Namespace="@(Namespace)">
  </Xml.ModifyFile>

  <Message Text="After modification"/>
</Target>
madgnome
A: 

Your XML File Is Invalid. I Tried It On My Machine , And It Worked Fine.

Just Close The bookstore Tag.

MJ