2009-02-02 67 views
5

我一直在使用Tigris社区任务来使用XMLUpdate任务更新各种AppSettings密钥。如何最好地更新MSBuild中的XML节点

现在,但是我想添加一个节点到system.net部分来设置代理。

我声明的属性

<PropertyGroup> 
    <proxy>&lt;defaultProxy&gt; &lt;proxy usesystemdefault="False" proxyaddress="http://IPADDRESS:PORT" /&gt; &lt;/defaultProxy&gt;</proxy> 
    </PropertyGroup> 

和任务的XMLUpdate貌似

<XmlUpdate 
Prefix="n" 
Namespace="http://schemas.microsoft.com/.NetConfiguration/v2.0" 
XmlFileName="$(BuildDir)\Builds\_PublishedWebsites\Presentation\Web.config" 
XPath="/n:configuration/n:system.net" 
Value="$(proxy)" /> 

这将更新web配置但它直接更新从属性组即不转换转义字符尖括号。有没有人有任何想法?

回答

7

您可以使用XmlMassUpdate而不是XmlUpdate任务。

<ProjectExtensions> 
    <defaultProxy> 
    <proxy usesystemdefault="False" proxyaddress="http://IPADDRESS:PORT"/> 
    </defaultProxy> 
</ProjectExtensions> 

<Target Name="SubstituteFromWebConfig"> 
    <XmlMassUpdate 
    NamespaceDefinitions="msb=http://schemas.microsoft.com/developer/msbuild/2003;n=http://schemas.microsoft.com/.NetConfiguration/v2.0" 
    ContentFile="$(BuildDir)\Builds\_PublishedWebsites\Presentation\Web.config" 
    ContentRoot="/n:configuration/n:system.net" 
    SubstitutionsFile="$(MSBuildProjectFullPath)" 
    SubstitutionsRoot="/msb:Project/msb:ProjectExtensions/msb:system.web" /> 
</Target> 

在这个例子中,我们通过一个由SubstitutionsRootSubstitutionsFile(当前的MSBuild文件)指出替换由ContentRootContentFile指向的节点。

这种技术利用了的MSBuild ProjectExtensions元素它允许您添加XML到项目文件将由MSBuild引擎被忽略的。

(或者如果您不想使用XmlMassUpdate,则可以在ProjectExtensions和XmlUpdate中的节点上使用XmlRead任务。)