Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过PowerShell保存XML时,始终会插入具有不一致行尾的注释_Xml_Powershell - Fatal编程技术网

通过PowerShell保存XML时,始终会插入具有不一致行尾的注释

通过PowerShell保存XML时,始终会插入具有不一致行尾的注释,xml,powershell,Xml,Powershell,我正在处理的项目有多个web..config文件。我发现像这样的元素中的变量只在主web.config文件中被替换,但它们需要在所有的.config文件中更新。我对此问题的解决方案是编写一个PowerShell脚本,该脚本在生成项目后接受%LAUNCHER\u PATH%和%LAUNCHER\u ARGS%的值,并更新其他.config文件 我编写的脚本按预期工作,但当我在更新后在Visual Studio中打开更新的文件时,应用程序会在运行脚本之前未显示此类警告时,抱怨该文件的行尾不一致。我已

我正在处理的项目有多个
web..config
文件。我发现像
这样的元素中的变量只在主web.config文件中被替换,但它们需要在所有的.config文件中更新。我对此问题的解决方案是编写一个PowerShell脚本,该脚本在生成项目后接受
%LAUNCHER\u PATH%
%LAUNCHER\u ARGS%
的值,并更新其他.config文件

我编写的脚本按预期工作,但当我在更新后在Visual Studio中打开更新的文件时,应用程序会在运行脚本之前未显示此类警告时,抱怨该文件的行尾不一致。我已经在Notepad++中确认,某些行只包含
LF
,而不包含
CR-LF
,它们与不断插入到我的文件中的注释相关

  <!--
    Configure your application settings in appsettings.json. Learn more at http://go.microsoft.com/fwlink/?LinkId=786380
  -->
正在读取并保存到中的XML

注释将插入到
的正上方


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

似乎您应该处理您可以控制的部分,即编写文件本身,以确保其换行符与Visual Studio添加的内容相匹配。似乎只有当文件的其余部分使用
CRLF
时,注释才添加到
LF

您可以通过使用自己的设置创建自己的
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保存到C:\Temp\web.config和C:\Temp\web1.config。我从web1.config中删除processPath和参数的值。我更新了
$folder='C:\Temp'
获取ChildItem$folder-filter web1.config
$xml.load($\uu.fullname)
,和
$xml.save($\uu.fullname)
,并取消了
$node
的3行注释。我运行了脚本,验证了这两个属性的设置是否正确,没有看到您看到的注释,也没有看到记事本++中任何格式错误的行尾。您是否在运行脚本后但在VS中打开项目之前检查记事本+?
<?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>
$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()
        }
    }
}