XmlPokeAliases.

XmlPoke(ICakeContext, FilePath, string, string, XmlPokeSettings) Method

Summary

Set the value of, or remove, target nodes.
Namespace
Cake.Common.Xml
Containing Type
XmlPokeAliases

Syntax

[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2202:Do not dispose objects multiple times")]
[CakeMethodAlias]
public static void XmlPoke(this ICakeContext context, FilePath filePath, string xpath, string value, XmlPokeSettings settings)

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>

Cake Task:

    Task("Transform")
    .Does(() =>
{
    var file = File("test.xml");
    XmlPoke(file, "/configuration/appSettings/add[@key = 'server']/@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>

Cake Task:

    Task("Transform")
    .Does(() =>
{
    var file = File("test.xml");
    XmlPoke(file, "/Commands/@xsi:noNamespaceSchemaLocation", "d:\\Commands.xsd", new XmlPokeSettings {
        Namespaces = new Dictionary<string, string> {
            { /* Prefix */ "xsi", /* URI */ "http://www.w3.org/2001/XMLSchema-instance" }
        }
    });
});

Remove an app setting from a config file.

XML file:

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

Cake Task:

    Task("Transform")
    .Does(() =>
{
    var file = File("test.xml");
    XmlPoke(file, "/configuration/appSettings/add[@testing]", null);
});

Attributes

Type Description
SuppressMessageAttribute
CakeMethodAliasAttribute An attribute used to mark script method aliases.

Parameters

Name Type Description
context ICakeContext The context.
filePath FilePath The target file.
xpath string The xpath of the nodes to set.
value string The value to set too. Leave blank to remove the selected nodes.
settings XmlPokeSettings Additional settings to tweak Xml Poke behavior.

Return Value

Type Description
void