尝试使用 MSBuild 任务来自 Microsoft.Sdc.Common.tasks

发布于 07-16 01:11 字数 4731 浏览 7 评论 0原文

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

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

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

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

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

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

谢谢/derdres

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

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

漫漫岁月2024-07-23 01:11:44

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

您只需将 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>

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>
无人问我粥可暖2024-07-23 01:11:44

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

只需关闭书店标签即可。

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

Just Close The bookstore Tag.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文