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.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Lou Franco
  • 87,846
  • 14
  • 132
  • 192

1 Answers1

9

Ok, I simplified the XML snippet above too much -- I think someone would have figured it out if I hadn't. The answer is that since Project has a namespace, it needs to be like this

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

For reference, the Project tag looks like this:

  <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build">
Lou Franco
  • 87,846
  • 14
  • 132
  • 192
  • 4
    1+ for finding out yourself. BTW: Don't use `//` unless you have to. If `x:Project` is the document element, use `/x:Project/...`. – Tomalak Apr 06 '10 at 14:01
  • 1
    Thanks @Tomalak. Just goes to show you that you shouldn't simplify the code too much in a question. You might be wrong about what's important, which is why you can't figure it out in the first place. – Lou Franco Apr 06 '10 at 15:46
  • Absolutely. I've seen many people making up code samples that are so simplified that they actually manage abstract their problem away. :-) – Tomalak Apr 06 '10 at 16:12