Search code examples
c#asp.netweb-configweb.config-transform

Delete an attribute in appSettings section with Web.config transformation


Is it possible to transform the following Web.config appSettings file:

<example Hostname="https://xxx.build" ClientSecretKey="myclientsecret" clientId="555" clientSecret="666"></example>

into something like this:

<example Hostname="https://xxx.build" ClientSecretKey="myclientsecret">/example>

I want to remove clientId and clientSecret from that setting


Solution

  • You can use the RemoveAttributes feature of web.config transformations for that. Your transform file would look like this:

    <example xdt:Transform="RemoveAttributes(clientId,clientSecret)">
    

    This will automatically remove those two attributes from the example element. An alternative approach is to replace the entire example element but leaving out the two attributes:

    <example Hostname="https://xxx.build" ClientSecretKey="myclientsecret" xdt:Transform="Replace"></example>
    

    Both transformations are tested and working with the Web.config Transformation Tester. There is more information about XDT and examples in this blog post: Web.config transformations - The definitive syntax guide.