17

我一直在为我的公司开发 NuGet 包,其中一项要求是能够更新我们的一些配置文件。

我知道可以添加到配置文件中,但是可以编辑一个吗?

例子:

<add name="conn" connectionString="Data Source=.\;Initial Catalog=DB;Integrated Security=True" />

更改为以下

<add name="conn" connectionString="Data Source=.\;Initial Catalog=DB;User ID=ex;Password=example" />
4

4 回答 4

30

NuGet 转换无法编辑现有值。但是 NuGet 允许您在安装包时运行 Powershell 脚本,因此您可以通过这种方式编辑配置文件。

创建一个 Install.ps1 文件并使用以下代码:

# Install.ps1
param($installPath, $toolsPath, $package, $project)

$xml = New-Object xml

# find the Web.config file
$config = $project.ProjectItems | where {$_.Name -eq "Web.config"}

# find its path on the file system
$localPath = $config.Properties | where {$_.Name -eq "LocalPath"}

# load Web.config as XML
$xml.Load($localPath.Value)

# select the node
$node = $xml.SelectSingleNode("configuration/connectionStrings/add[@name='gveconn']")

# change the connectionString value
$node.SetAttribute("connectionString", "Data Source=.\;Initial Catalog=GVE;User ID=ex;Password=example")

# save the Web.config file
$xml.Save($localPath.Value)
于 2011-12-09T17:59:15.743 回答
16

从 NuGet 2.6 及更高版本开始,您实际上可以使用用于 Visual Studio 中 Web.config 转换的 XDT 语法来转换 Web.config 文件。

请参阅http://docs.nuget.org/docs/creating-packages/configuration-file-and-source-code-transformations

支持 XML 文档转换 (XDT)

从 NuGet 2.6 开始,支持 XDT 在项目内转换 XML 文件。XDT 语法可以在包的 Content 文件夹下的 .install.xdt 和 .uninstall.xdt 文件中使用,它们将分别在包安装和卸载时应用。

例如,要将 MyNuModule 添加到 web.config 文件中,如上所示,可以在 web.config.install.xdt 文件中使用以下部分:

<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
    <system.webServer>
        <modules>
            <add name="MyNuModule" type="Sample.MyNuModule" xdt:Transform="Insert" />
        </modules>
    </system.webServer>
</configuration>

另一方面,要在软件包卸载期间仅删除 MyNuModule 元素,可以在 web.config.uninstall.xdt 文件中使用以下部分:

<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
    <system.webServer>
        <modules>
            <add name="MyNuModule" xdt:Transform="Remove" xdt:Locator="Match(name)" />
        </modules>
    </system.webServer>
</configuration>
于 2013-10-16T09:19:55.170 回答
3

编辑:从NUGET 2.6 及更高版本开始,答案现在是肯定的。

答案是否定的。从nuget网站我找到了以下答案:

“当 NuGet 将转换文件合并到项目的配置文件中时,它只会向配置文件中的现有元素添加元素或添加属性;它不会以任何其他方式更改现有元素或属性。”

http://docs.nuget.org/docs/creating-packages/configuration-file-and-source-code-transformations

于 2011-08-01T17:19:14.330 回答
0

是的,这是可能的,但您必须将install.ps1文件包含到工具文件夹中。然后,当您从 nuget 服务器获取包时,Visual Studio 会运行​​ Powershell 脚本。我使用这个脚本

# fileName can be App.Config Or Web.Config or something else 
$fileName = "App.Config" 
$file=$project.ProjectItems.Item($fileName)

if($file.Properties){
    # Get localpath
    $localPath = $file.Properties.Item("LocalPath")
    if($localPath){
        $localPath = $localPath.Value   
    }
}

if ($localPath -eq $null) {
    Exit
}

#Load our config file as XML file
[xml]$file = Get-Content $localPath
if($file){

    # Create node 
    $childNode = $file.CreateElement("add")
    $childNode.SetAttribute("connectionString", "DataSource=.\;InitialCatalog=GVE;User ID=ex;Password=example")

    #Get parent node   
    $node = $file.SelectSingleNode("configuration/connectionStrings")

    #Insert our node into parent
    $node.AppendChild($childNode)

    $file.Save($localPath)
}
于 2016-03-16T13:32:15.827 回答