0

我对 Xml.ModifyFile 任务有疑问,我不明白。你们能帮忙吗?

我的目标只是操作 xml 文档中的属性。

我对 xml 尤其是 msbuild 的世界相当陌生,因此我很难解释我收到的错误消息。在我看来,我的构建文件是有效的,所以我猜 sdc.tasks dll 文件有问题。

从构建文件中可以看出,为了测试,我添加了一个名为“ping”的目标。该目标与 sdc.task Ping 一起使用没有任何问题

你们能建议一个修复或替代解决方案来解决使用 msbuild 修改 xml 文件的挑战吗?

另一个问题 - 如何将多个命名空间声明为 Xml.ModifyFile sdc.task 的参数?namespace 属性的解释如下: TaskItems 数组,指定“Prefix”和“Uri”属性,用于指定的 xPath。我试图找到 taskitems 用法的解释或示例,但不幸的是没有任何运气。

谢谢/derdres

我将在下面列出以下内容:

  1. 构建文件
  2. 我尝试修改的 xml 文件
  3. 错误信息

1) 构建文件

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

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

2 回答 2

2

在您的 XPath 中,您正在搜索属性年龄/bookstore/book[@id=2]/@age,但在您的任务中,您将 AttributeName 设置为“年龄”。所以这就像你想要属性年龄的属性年龄。

您只需要更改您的 XPath 以/bookstore/book[@id=2]使其工作。

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

如何将多个命名空间声明为 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>
于 2009-04-07T06:21:06.350 回答
0

您的 XML 文件无效。我在我的机器上试过了,效果很好。

只需关闭书店标签。

于 2009-10-27T11:26:30.960 回答