<xmlpoke>

Replaces text in an XML file at the location specified by an XPath expression.

The location specified by the XPath expression must exist, it will not create the parent elements for you. However, provided you have a root element you could use a series of the tasks to build the XML file up if necessary.

Parameters

Attribute Type Description Required
value string
The value that replaces the contents of the selected nodes.
True
file file
The name of the file that contains the XML document that is going to be poked.
True
xpath string
The XPath expression used to select which nodes are to be modified.
True
preserveWhitespace bool
If true then the whitespace in the resulting document will be preserved; otherwise the whitespace will be removed. The default is false.
False
failonerror bool
Determines if task failure stops the build, or is just reported. The default is true.
False
if bool
If true then the task will be executed; otherwise, skipped. The default is true.
False
unless bool
Opposite of NAnt.Core.Task.IfDefined. If false then the task will be executed; otherwise, skipped. The default is false.
False
verbose bool
Determines whether the task should report detailed build log messages. The default is false.
False

Nested elements

<namespaces>

Namespace definitions to resolve prefixes in the XPath expression.

Parameters

Attribute Type Description Required
prefix string
The prefix to associate with the namespace.
True
uri string
The associated XML namespace URI.
True
if bool
Indicates if the namespace should be added to the System.Xml.XmlNamespaceManager. If true then the namespace will be added; otherwise, skipped. The default is true.
False
unless bool
Indicates if the namespace should not be added to the System.Xml.XmlNamespaceManager. list. If false then the parameter will be added; otherwise, skipped. The default is false.
False

<namespaces>

Examples

Change the server setting in the configuration from testhost.somecompany.com to productionhost.somecompany.com.

XML file:

    <?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <appSettings>
        <add key="server" value="testhost.somecompany.com" />
    </appSettings>
</configuration>

Build fragment:

<xmlpoke
file="App.config"
xpath="/configuration/appSettings/add[@key = 'server']/@value"
value="productionhost.somecompany.com" />

Modify the noNamespaceSchemaLocation in an XML file.

XML file:

    <?xml version="1.0" encoding="utf-8" ?>
<Commands xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Path Value">
</Commands>

Build fragment:

    <xmlpoke file="test.xml" xpath="/Commands/@xsi:noNamespaceSchemaLocation" value="d:\Commands.xsd">
    <namespaces>
        <namespace prefix="xsi" uri="http://www.w3.org/2001/XMLSchema-instance" />
    </namespaces>
</xmlpoke>

Requirements

Assembly
NAnt.Core.dll
Namespace
NAnt.Core.Tasks