Using xmlpeek in Nant script gives an odd error

As part of the CI process, I am trying to create a buildlabel that consists of the contents of an xml element in an xml structure. For this, I use nant and xmlpeek. My problem is that I get a weird error:

"Nodeindex" 0 "out of range"

This is only if the xml file that I am xmlpeeking contains a namespace definition in the root of the node.

Removing the namespace from the XML file gives me the result that I expect.

The redirected target that generates the error can be reduced to:

<target name="TDSLabel"> <property name="element" value=""/> <echo message="Getting element" /> <xmlpeek file="C:\xxx\test1.xml" xpath="//Project/PropertyGroup/ProductVersion" property="element"/> <echo message="The found element value was: ${element}" /> </target> 

and test1.xml looks like this:

 <Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <PropertyGroup> <ProductVersion>9.0.21022</ProductVersion> </PropertyGroup> </Project> 
+6
source share
2 answers

You have already given the right hint. This is about the namespace. This should fix this:

 <target name="TDSLabel"> <property name="element" value=""/> <echo message="Getting element" /> <xmlpeek file="C:\xxx\test1.xml" xpath="//x:Project/x:PropertyGroup/x:ProductVersion" property="element" verbose="true"> <namespaces> <namespace prefix="x" uri="http://schemas.microsoft.com/developer/msbuild/2003" /> </namespaces> </xmlpeek> <echo message="The found element value was: ${element}" /> </target> 
+5
source

Found a similar problem and applied my problem: XmlPoke and unique nodes . The problem was that I did not include the namespace definition in the xmlpeek element and subsequently omitted the necessary namespace reference in my xpath statement:

 <xmlpeek file="C:\xxx\test1.xml" xpath="//x:Project/x:PropertyGroup/x:ProductVersion" property="element"> <namespaces> <namespace prefix="x" uri="http://schemas.microsoft.com/developer/msbuild/2003" /> </namespaces> </xmlpeek> 
0
source

Source: https://habr.com/ru/post/911236/


All Articles