5

I am trying to use an xmlpoke task to update a VS Project File (which is XML). In the Project root, there are multiple PropertyGroup nodes, I am trying to select the first one. The XML looks like this

 <Project>
    <PropertyGroup>
    </PropertyGroup>
    <PropertyGroup>
    </PropertyGroup>
    <PropertyGroup>
    </PropertyGroup>
 </Project>

I am using an xpath of //Project/PropertyGroup[1] to get the first PropertyGroup, but I get the error: “Non-unique xpath given //Project/PropertyGroup[1]”.

edit: sorry, I didn't think it mattered (but it does), Project has a namespace. I put the correct XML with the correct xmlpoke as an answer for any future searchers.

4

1 回答 1

9

好的,我将上面的 XML 片段简化得太多了——我想如果我没有的话,有人会想出来的。答案是,既然Project有命名空间,就需要是这样的

   <xmlpoke file="project_file.csproj" value="v4.0" xpath="//x:Project/x:PropertyGroup[1]/x:TargetFrameworkVersion">
    <namespaces>
      <namespace prefix="x" uri="http://schemas.microsoft.com/developer/msbuild/2003" />
    </namespaces>
   </xmlpoke>

作为参考,Project 标签如下所示:

  <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build">
于 2010-04-06T13:47:08.367 回答