Web.Config Transformation ASP.NET 4.0 新特性

Web.Config Transformation详解,这部分内容比较简单,关键是用没有用过的问题,所以这里希望帮助大家实践一下。


一 概述:

在VS2010中引入在Config 文件中使用XML DOCUMENT TRANSFORM,这一个特性就是帮助你Web.config能方便的从部署配置文件转化到产品配置文件。协助Web.Config Transformation这两个功能就是web.debug.config, web.release.config等.这些文件的最后都会匹配到MSBuild中的配置文件去。

其实是在web.debug.config和web.release.config来写一些描述文件,再通过Transformation Engine来转化。

在Transformation Engine 执行下面任务

首先是识别Locator属性是否设置,来判断是否使用XML转换,接着就是从原配置的XML文件中获得相应节点,再从转换的XML文件中招到适合Transform的值相匹配的节点,然后将他们转化到指定的XML配置文件。在转化中主要依赖的是Transform的attribute.



 

二:实践




基础部分:

要使用XML-Document-Transform engine就要先引用XML-Document-Transform 命名空间,如果在你的Conifg文件中引用这个命名空间,你就能在本Web.config中使用转换描述。


要使用XML-Document-Transform engine就要先引用XML-Document-Transform 命名空间,如果在你的Conifg文件中引用这个命名空间,你就能在本Web.config中使用转换描述。

下面是在web.release.config中使用


<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">


接下来是使用Locator,Locator是代表一组表达式,主要是基于 XPath的,通过配置Locator来查找Web.Config.并做相应的事情。

1 :locator属性

下面有个表,来详细列举locator的语法

(1)Match;

         这里你需要就是在你直接匹配的属性名。     

<connectionStrings>
  <add name="Northwind" connectionString="connection string detail"
    providerName="System.Data.SqlClient" 
    xdt:Transform="Replace" 
    xdt:Locator="Match(name)" />
</connectionStrings>


Engine会再你的Web.config中找到匹配name为Norhwind的就用上面的配置文件图替换。

(2)Condition
基于XPath,在Locator中应用有逻辑性的判断表达式。

<connectionStrings>
  <add name="Northwind" 
    connectionString="connection string detail" 
    providerName="System.Data.SqlClient" 
    xdt:Transform="Replace" 
    xdt:Locator="Condition(@name=’Northwind or @providerName=' System.Data.SqlClient')" />
</connectionStrings>


上面就是Name属性匹配‘Norhwind’的或providerName匹配System.Data.SqlClient的配置文件节点都会被替换。

(3)XPath
这个就是直接写XPath,http://www.w3.org/TR/xpath,这里是XPath的标准

<location path="c:\MySite\Admin" >
  <system.web xdt:Transform="Replace" xdt:Locator="XPath(//system.web)">
     
  </system.web>
<location>


这里你会发现,这里可以写一些列的表达式。

2: Transform 属性

(1) Replace
表示所有匹配的节点都是替换

<assemblies xdt:Transform="Replace">
  <add assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
</assemblies>


其实这里描述文件时web.release.config,将要替换的文件时Web.config .

(2) Remove

删除第一匹配的元素。

<assemblies xdt:Transform="Remove">
</assemblies>

 

(3)RemoveAll

删除所有匹配的元素

 

<connectionStrings> 
  <add xdt:Transform="RemoveAll"/>
</connectionStrings>


(4)Insert

插入从父节点中插入,(authorization中插入<deny users="*" />)

 

<authorization>
  <deny users="*" xdt:Transform="Insert"/>
</authorization>

 

(5)SetAttributes

直接设置Attributes

<compilation 
    batch="false"
    xdt:Transform="SetAttributes(batch)">
</compilation>


(6)RemoveAttributes
删除出Attributes

<compilation xdt:Transform="RemoveAttributes(debug,batch)">
</compilation>

 

 

(7)InsertAfter (XPath)
通过匹配 XPath的表达式的,找到节点,并子节点后面插入 XML

<authorization>
  <deny users="AName" xdt:Transform="InsertAfter(/configuration/system.web/authorization/ allow[@roles='Admins']") />
</authorization>

(8)InsertBefore (XPath)
通过匹配 XPath的表达式的,找到节点,并子节点前面插入 XML

 


<authorization>
  <allow roles=" Admins" xdt:Transform="InsertBefore(/configuration/system.web/authorization/ deny[@users='*'])" />
</authorization>


(9)XSLT (filePath)

可以在外部定义 XSLT文件,来替换Web.cofig文件。

<appSettings xdt:Transform="XSLT(V:\MyProject\appSettings.xslt)">
</appSettings>


 

总结(Summarize)


ASP.NET 4.0中这个特性,主要能帮助大家的应用程序中的配置文件能从Debug平缓的转换到发布配置文件,而所以想的要从开发到产品的发布的转化,这只是其中的一部分,总的看这里主要就写一个描述文件,而描述文件如何和目标文件关联的,就是使用的Transform和Locator这两个属性来控制,再通过XML-Document-Tranformation engine来将他们粘合在一起。

Transform和Locator都做下面这些事情:
Transform :

Replacing a node

Inserting a node

Delete a node

Removing Attributes

Setting Attributes

Locator;

Match on value of a node’s attribute

Exact XPath of where to find a node

A condition match to find a node

最后别忘记, 他们是xdt扩展元素: xdt:Transform



参考
www.msdn.com
http://weblogs.asp.net/gunnarpeipman/archive/2009/06/16/visual-studio-2010-web-config-transforms.aspx
http://weblogs.asp.net/gunnarpeipman/archive/2009/06/16/visual-studio-2010-web-config-transforms.aspx

转载于:https://www.cnblogs.com/chengjie/archive/2012/02/20/2360608.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用 Microsoft.Web.XmlTransform 升级 XML 文件的示例: 假设我们有一个名为 app.config 的 XML 文件,其中包含一个名为 "connectionString" 的节点,我们想要在部署到生产服务器时更改该节点的值。我们可以使用 Microsoft.Web.XmlTransform 来实现这一点。 首先,我们需要创建一个新的 XML 转换文件(XDT 文件),其中包含要应用于 XML 文件的转换规则。在这种情况下,我们需要更改 connectionString 节点的值,因此,我们可以使用 SetAttributes 元素来更改该节点的属性。以下是 XDT 文件的示例: ```xml <?xml version="1.0"?> <Transformation xmlns="http://schemas.microsoft.com/XML-Document-Transform"> <SetAttributes> <connectionStrings> <add name="MyDB" connectionString="new_connection_string" providerName="System.Data.SqlClient" /> </connectionStrings> </SetAttributes> </Transformation> ``` 在上面的示例中,我们使用 SetAttributes 元素更改了 connectionString 节点的值。我们添加了一个新的 add 元素,并为其提供了新的 connectionString 值。请注意,我们还需要提供 providerName 属性的值,因为该属性是 connectionString 节点的必需属性。 接下来,我们需要在应用程序中使用 XmlTransformation 类将 XDT 文件应用于 XML 文件。以下是示例代码: ```csharp using Microsoft.Web.XmlTransform; // Load the XML file to transform var xmlDoc = new System.Xml.XmlDocument(); xmlDoc.Load("path/to/app.config"); // Load the XDT file var transform = new XmlTransformation("path/to/transform.xdt"); // Apply the transformation to the XML file if (transform.Apply(xmlDoc)) { xmlDoc.Save("path/to/transformed/config/file.xml"); } ``` 在上面的示例中,我们使用 XmlTransformation 类将 XDT 文件应用于 app.config 文件。如果转换成功,我们将保存转换后的 XML 文件到指定路径中。 希望这个示例可以帮助你了解如何使用 Microsoft.Web.XmlTransform 升级 XML 文件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值