gpt4 book ai didi

xml - 通过 PowerShell 保存 XML 总是插入行结尾不一致的注释

转载 作者:行者123 更新时间:2023-12-02 23:53:15 24 4
gpt4 key购买 nike

我正在做的项目有多个 web.<env>.config文件。我发现像 <aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" /> 这样的元素中的变量仅在主 web.config 文件中被替换,但它们需要在所有 .config 文件中更新。我对这个问题的解决方案是编写一个 PowerShell 脚本,其值为 %LAUNCHER_PATH%。和 %LAUNCHER_ARGS%项目建成后更新其他 .config 文件。

我编写的脚本按预期工作,但是当我在更新后在 Visual Studios 中打开更新的文件时,应用程序提示在运行脚本之前没有出现此类警告时,该文件的行结尾不一致。我已在 Notepad++ 中确认某些行仅包含 LF而不是 CR LF它们与不断插入我的文件的这条评论有关。

  <!--
Configure your application settings in appsettings.json. Learn more at http://go.microsoft.com/fwlink/?LinkId=786380
-->

我发现无论我做什么,这条评论都会不断地插入到我的文件中;即使我只加载并立即保存文件。

脚本
# Updates web.{env}.config files with the correct processPath and arguments
# that are populated in web.config during the publish process

$folder = "."
$nodeXpath = "configuration/system.webServer/aspNetCore"

if (Test-Path "$folder\web.config")
{
$originalNode = Select-Xml -Path "$folder\web.config" -XPath $nodeXpath | Select-Object -ExpandProperty Node
$processPath = Select-Xml -XPath "@processPath" -Xml $originalNode
$arguments = Select-Xml -XPath "@arguments" -Xml $originalNode

Get-ChildItem $folder -Filter web.*.config |
Foreach-Object {
# Using .NET XmlDocument
$xml = New-Object System.Xml.XmlDocument
$xml.PreserveWhitespace = $true
$xml.Load($_)
#$node = Select-Xml -Xml $xml -XPath $nodeXpath | Select-Object -ExpandProperty Node
#$node.SetAttribute("processPath", $processPath)
#$node.SetAttribute("arguments", $arguments)
$xml.Save((Resolve-Path $_))
}
}

XML 被读取并保存到
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified"/>
</handlers>
<aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false"/>
<security>
<authorization>
<remove users="*" roles="" verbs="" />
<add accessType="Allow" users="blah" />
<add accessType="Allow" roles="blahblah" />
<add accessType="Allow" roles="blahblahblah" />
</authorization>
<authentication>
<windowsAuthentication enabled="true" />
<anonymousAuthentication enabled="false" />
</authentication>
</security>
</system.webServer>
</configuration>

评论将插入到 <system.webServer> 的正上方.

有没有办法强制保存操作不插入评论?如果这是不可能的,有没有办法纠正注释,使其与文件的其余部分相比没有不一致的行尾?

最佳答案

似乎您应该处理您可以控制的部分,即编写文件本身,以确保其换行符与 Visual Studio 添加的内容相匹配。似乎评论正在添加 LF仅当您的文件的其余部分正在使用 CRLF 时.

您可以通过创建自己的XmlWriter 来控制输出。有自己的设置:

$folder = "."
$nodeXpath = "configuration/system.webServer/aspNetCore"

if (Test-Path "$folder\web.config")
{
$originalNode = Select-Xml -Path "$folder\web.config" -XPath $nodeXpath | Select-Object -ExpandProperty Node
$processPath = Select-Xml -XPath "@processPath" -Xml $originalNode
$arguments = Select-Xml -XPath "@arguments" -Xml $originalNode

$settings = [System.Xml.XmlWriterSettings]::new()
$settings.NewLineChars = "`n"

Get-ChildItem $folder -Filter web.*.config |
Foreach-Object {
# Using .NET XmlDocument
$xml = New-Object System.Xml.XmlDocument
$xml.PreserveWhitespace = $true
$xml.Load($_)
#$node = Select-Xml -Xml $xml -XPath $nodeXpath | Select-Object -ExpandProperty Node
#$node.SetAttribute("processPath", $processPath)
#$node.SetAttribute("arguments", $arguments)

$writer = [System.Xml.XmlWriter]::Create((Resolve-Path $_), $settings)
try {
$xml.Save($writer)
} finally {
$writer.Close()
}
}
}

关于xml - 通过 PowerShell 保存 XML 总是插入行结尾不一致的注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52750342/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com