As part of a CI process I am trying to create a buildlabel which consists of the content of an xml element within an xml structure. For this purpose I am using nant and xmlpeek. My problem is that I get an odd error stating:
"Nodeindex '0' is out of range"
This is only the case if the xml file I am xmlpeeking contains a namespace definition in the root node.
Removing the namespace from the xml file gives me the output I expect.
The nant target that generates the error can be boild down 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 the test1.xml file 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>
                You already gave the right hint yourself. It's about the namespace. This should fix it:
<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>
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With