Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1061675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:30:31+00:00 2026-05-16T18:30:31+00:00

Does anyone know if there is a way to "transform" specific sections of values

  • 0

Does anyone know if there is a way to "transform" specific sections of values instead of replacing the whole value or an attribute?

For example, I’ve got several appSettings entries that specify the URLs for different webservices. These entries are slightly different in the dev environment than the production environment. Some are less trivial than others

<!-- DEV ENTRY -->
<appSettings>
 <add key="serviceName1_WebsService_Url" value="http://wsServiceName1.dev.example.com/v1.2.3.4/entryPoint.asmx" />
 <add key="serviceName2_WebsService_Url" value="http://ma1-lab.lab1.example.com/v1.2.3.4/entryPoint.asmx" />
</appSettings>

<!-- PROD ENTRY -->
<appSettings>
 <add key="serviceName1_WebsService_Url" value="http://wsServiceName1.prod.example.com/v1.2.3.4/entryPoint.asmx" />
 <add key="serviceName2_WebsService_Url" value="http://ws.ServiceName2.example.com/v1.2.3.4/entryPoint.asmx" />
</appSettings>

Notice that on the fist entry, the only difference is ".dev" from ".prod". On the second entry, the subdomain is different: "ma1-lab.lab1" from "ws.ServiceName2"

So far, I know I can do something like this in the Web.Release.Config:

<add xdt:Locator="Match(key)" xdt:Transform="SetAttributes(value)" key="serviceName1_WebsService_Url" value="http://wsServiceName1.prod.example.com/v1.2.3.4/entryPoint.asmx" />
<add xdt:Locator="Match(key)" xdt:Transform="SetAttributes(value)" key="serviceName2_WebsService_Url" value="http://ws.ServiceName2.example.com/v1.2.3.4/entryPoint.asmx" />

However, every time the Version for that webservice is updated, I would have to update the Web.Release.Config as well, which defeats the purpose of simplfying my web.config updates.

I know I could also split that URL into different sections and update them independently, but I rather have it all in one key.

I’ve looked through the available web.config Transforms but nothings seems to be geared towars what I am trying to accomplish.

These are the websites I am using as a reference:

Vishal Joshi’s blog, MSDN Help, and Channel9 video

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T18:30:31+00:00Added an answer on May 16, 2026 at 6:30 pm

    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 http://sedodream.com/2010/09/09/ExtendingXMLWebconfigConfigTransformation.aspx regarding this. But here are the hightlights:

    • Create Class Library project
    • Reference Web.Publishing.Tasks.dll (under %Program Files (x86)%MSBuild\Microsoft\VisualStudio\v10.0\Web folder)
    • Extend Microsoft.Web.Publishing.Tasks.Transform class
    • Implement the Apply() method
    • Place the assembly in a well known location
    • Use xdt:Import to make the new transform available
    • Use transform

    Here is the class I created to do this replace

    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 && this.Arguments.Count > 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 <= 0 || last <= 0 || last <= 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);
                    }
                }
            }
        }
    }
    

    Here is web.config

    <?xml version="1.0"?>
    <configuration>
      <appSettings>
        <add key="one" value="one"/>
        <add key="two" value="partial-replace-here-end"/>
        <add key="three" value="three here"/>
      </appSettings>
    </configuration>
    

    Here is my config transform file

    <?xml version="1.0"?>
    
    <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
    
      <xdt:Import path="C:\Program Files (x86)\MSBuild\Custom\CustomTransformType.dll"
                  namespace="CustomTransformType" />
    
      <appSettings>
        <add key="one" value="one-replaced" 
             xdt:Transform="Replace" 
             xdt:Locator="Match(key)" />
        <add key="two" value="two-replaced" 
             xdt:Transform="AttributeRegexReplace(Attribute='value', Pattern='here',Replacement='REPLACED')" 
             xdt:Locator="Match(key)"/>
      </appSettings>
    </configuration>
    

    Here is the result after transformation

    <?xml version="1.0"?>
    <configuration>
      <appSettings>
        <add key="one" value="one-replaced"/>
        <add key="two" value="partial-replace-REPLACED-end"/>
        <add key="three" value="three here"/>
      </appSettings>
    </configuration>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.