XmlPokeAliases.

XmlPokeString(ICakeContext, string, string, string) Method

Summary

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

Syntax

[CakeMethodAlias]
public static string XmlPokeString(this ICakeContext context, string sourceXml, string xpath, string value)

Examples

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

XML string:

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

Cake Task:

    Task("Transform")
    .Does(() =>
{
    var result = XmlPokeString(xmlString, "/configuration/appSettings/add[@key = 'server']/@value", "productionhost.somecompany.com");
});

Modify the noNamespaceSchemaLocation in an XML file.

XML string:

    <?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 result = XmlPokeString(xmlString, "/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 string:

    <?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 result = XmlPokeString(xmlString, "/configuration/appSettings/add[@testing]", null);
});

Attributes

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

Parameters

Name Type Description
context ICakeContext The context.
sourceXml string The source xml to transform.
xpath string The xpath of the nodes to set.
value string The value to set too. Leave blank to remove the selected nodes.

Return Value

Type Description
string Resulting XML.