事实上,您可以这样做,但并不像您想象的那么容易。您可以创建自己的配置转换。我刚刚在
http://sedodream.com/2010/09/09/ExtendingXMLWebconfigConfigTransformation.aspx上写了一篇非常详细的博客文章。以下是要点:
- 创建类库项目
- 参考Web.Publishing.Tasks.dll(位于%Program Files (x86)%MSBuild\Microsoft\VisualStudio\v10.0\Web文件夹下)
- 扩展Microsoft.Web.Publishing.Tasks.Transform类
- 实现Apply()方法
- 将程序集放置在众所周知的位置
- 使用xdt:Import使新的转换可用
- 使用transform
这是我创建的用于替换的类
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)
{
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)
{
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)
{
att.Value = Regex.Replace(att.Value, this.Pattern, this.Replacement);
}
}
}
}
}
这是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>
这是我的配置转换文件
<?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>
转换后的结果如下:
<?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>
protected override void Apply() { foreach (XmlNode target in this.TargetNodes) { foreach (XmlAttribute att in target.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); } } } }
- Brad Wilkie