XmlPeek task

Returns values as specified by XPath Query from an XML file.


The following table describes the parameters of the XmlPeek task.

Parameter Description
Namespaces Optional String parameter.

Specifies the namespaces for the XPath query prefixes.
Query Optional String parameter.

Specifies the XPath query.
Result Optional ITaskItem[] output parameter.

Contains the results that are returned by this task.
XmlContent Optional String parameter.

Specifies the XML input as a string.
XmlInputPath Optional ITaskItem parameter.

Specifies the XML input as a file path.


In addition to having the parameters that are listed in the table, this task inherits parameters from the TaskExtension class, which itself inherits from the Task class. For a list of these additional parameters and their descriptions, see TaskExtension base class.


Here is a sample XML file settings.config to read:

  <add key="ProjectFolder" value="S1" />

In this example, if you want to read value, then use code like the following:

<Target Name="BeforeBuild">
    <XmlPeek XmlInputPath="settings.config" Query="appSettings/add[@key='ProjectFolder']/@value">
        <Output TaskParameter="Result" ItemName="value" />
    <Message Text="Using project folder @(value)." Importance="high" />
        <Compile Include="Projects\$(ProjectFolder)\Controls\Control1.ascx.cs">

With XML namespaces, you use the Namespaces parameter, as in the following example. With the input XML file XMLFile1.xml:

<?xml version="1.0" encoding="UTF-8"?>
<class AccessModifier='public' Name='test' xmlns:s='http://nsurl'>
  <s:variable Type='String' Name='a'>This</s:variable>
  <s:variable Type='String' Name='b'>is</s:variable>
  <s:variable Type='String' Name='c'>Sparta!</s:variable>
  <method AccessModifier='public static' Name='GetVal' />

And the following Target defined in a project file:

  <Target Name="TestPeek" BeforeTargets="Build">
    <!-- Find the Name attributes -->
    <XmlPeek XmlInputPath="XMLFile1.xml"
             Namespaces="&lt;Namespace Prefix='s' Uri='http://nsurl' /&gt;">
      <Output TaskParameter="Result" ItemName="value1" />
    <Message Text="@(value1)"/>
    <!-- Find 'variable' nodes (XPath query includes ".") -->
    <XmlPeek XmlInputPath="XMLFile1.xml"
             Namespaces="&lt;Namespace Prefix='s' Uri='http://nsurl' /&gt;">
      <Output TaskParameter="Result" ItemName="value2" />
    <Message Text="@(value2)"/>

The output includes the following from the TestPeek target:

  TestPeek output:
  <s:variable Type="String" Name="a" xmlns:s="http://nsurl">This</s:variable>;<s:variable Type="String" Name="b" xmlns:s="http://nsurl">is</s:variable>;<s:variable Type="String" Name="c" xmlns:s="http://nsurl">Sparta!</s:variable>

See also