>我只是加载xml文件并将其保存到新文件中。这将修改属性的格式,并在value="foo"/>
到value="foo" />
我知道PreserveWhitespace
不支持格式化属性。
有什么方法可以避免这些空格/格式出现在XML行的末尾吗?
Powershell脚本
$filePath = "C:BeforeSave.xml"
[xml]$apptypexml = New-Object System.Xml.XmlDocument
$apptypexml.PreserveWhitespace = $true
[xml] $apptypexml = [xml] (Get-Content $filePath)
$apptypexml.Save("C:AfterSave.xml")
保存前的示例 XML
<?xml version="1.0"?>
<ApplicationTypes>
<executable>
<Item type="registry" value="HKEY_LOCAL_MACHINE"/>
<Item type="string" value="Sample.exe"/>
</executable>
</ApplicationTypes>
保存后输出 XML
<?xml version="1.0"?>
<ApplicationTypes>
<executable>
<Item type="registry" value="HKEY_LOCAL_MACHINE "/> <!-- Powershell is giving spaces in the end of attributes -->
<Item type="string" value="Sample.exe" /> <!-- Powershell is giving spaces in the end of attributes -->
</executable>
</ApplicationTypes>
Hacky 解决方案将是一个简单的文本替换:
(Get-Content -Path "C:AfterSave.xml") -replace '" />','"/>' -replace ' "/>','"/>' | Set-Content "C:AfterSave.xml"