Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As a matter of fact you can do this, but it is not as easy as you might think. You can create your own config transformation. I have just written a very detailed blog post at <a href="http://sedodream.com/2010/09/09/ExtendingXMLWebconfigConfigTransformation.aspx" rel="noreferrer">http://sedodream.com/2010/09/09/ExtendingXMLWebconfigConfigTransformation.aspx</a> regarding this. But here are the hightlights:</p> <ul> <li>Create Class Library project</li> <li>Reference Web.Publishing.Tasks.dll <em>(under %Program Files (x86)%MSBuild\Microsoft\VisualStudio\v10.0\Web folder)</em></li> <li>Extend Microsoft.Web.Publishing.Tasks.Transform class</li> <li>Implement the Apply() method</li> <li>Place the assembly in a well known location</li> <li>Use xdt:Import to make the new transform available</li> <li>Use transform</li> </ul> <p><strong>Here is the class I created to do this replace</strong></p> <pre><code>namespace CustomTransformType { using System; using System.Text.RegularExpressions; using System.Xml; using Microsoft.Web.Publishing.Tasks; public class AttributeRegexReplace : Transform { private string pattern; private string replacement; private string attributeName; protected string AttributeName { get { if (this.attributeName == null) { this.attributeName = this.GetArgumentValue("Attribute"); } return this.attributeName; } } protected string Pattern { get { if (this.pattern == null) { this.pattern = this.GetArgumentValue("Pattern"); } return pattern; } } protected string Replacement { get { if (this.replacement == null) { this.replacement = this.GetArgumentValue("Replacement"); } return replacement; } } protected string GetArgumentValue(string name) { // this extracts a value from the arguments provided if (string.IsNullOrWhiteSpace(name)) { throw new ArgumentNullException("name"); } string result = null; if (this.Arguments != null &amp;&amp; this.Arguments.Count &gt; 0) { foreach (string arg in this.Arguments) { if (!string.IsNullOrWhiteSpace(arg)) { string trimmedArg = arg.Trim(); if (trimmedArg.ToUpperInvariant().StartsWith(name.ToUpperInvariant())) { int start = arg.IndexOf('\''); int last = arg.LastIndexOf('\''); if (start &lt;= 0 || last &lt;= 0 || last &lt;= 0) { throw new ArgumentException("Expected two ['] characters"); } string value = trimmedArg.Substring(start, last - start); if (value != null) { // remove any leading or trailing ' value = value.Trim().TrimStart('\'').TrimStart('\''); } result = value; } } } } return result; } protected override void Apply() { foreach (XmlAttribute att in this.TargetNode.Attributes) { if (string.Compare(att.Name, this.AttributeName, StringComparison.InvariantCultureIgnoreCase) == 0) { // get current value, perform the Regex att.Value = Regex.Replace(att.Value, this.Pattern, this.Replacement); } } } } } </code></pre> <p><strong>Here is web.config</strong></p> <pre><code>&lt;?xml version="1.0"?&gt; &lt;configuration&gt; &lt;appSettings&gt; &lt;add key="one" value="one"/&gt; &lt;add key="two" value="partial-replace-here-end"/&gt; &lt;add key="three" value="three here"/&gt; &lt;/appSettings&gt; &lt;/configuration&gt; </code></pre> <p>Here is my config transform file</p> <pre><code>&lt;?xml version="1.0"?&gt; &lt;configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"&gt; &lt;xdt:Import path="C:\Program Files (x86)\MSBuild\Custom\CustomTransformType.dll" namespace="CustomTransformType" /&gt; &lt;appSettings&gt; &lt;add key="one" value="one-replaced" xdt:Transform="Replace" xdt:Locator="Match(key)" /&gt; &lt;add key="two" value="two-replaced" xdt:Transform="AttributeRegexReplace(Attribute='value', Pattern='here',Replacement='REPLACED')" xdt:Locator="Match(key)"/&gt; &lt;/appSettings&gt; &lt;/configuration&gt; </code></pre> <p><strong>Here is the result after transformation</strong></p> <pre><code>&lt;?xml version="1.0"?&gt; &lt;configuration&gt; &lt;appSettings&gt; &lt;add key="one" value="one-replaced"/&gt; &lt;add key="two" value="partial-replace-REPLACED-end"/&gt; &lt;add key="three" value="three here"/&gt; &lt;/appSettings&gt; &lt;/configuration&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload