我正在使用Powershell更新一些xml文件,它们是从Linux机器产生的。一旦我完成更新,文件就被多余的空格弄乱了等等,我不能使用它。
Changes from:
UNIX )(LF) UTF-8
To
Windows (CR LF) UTF-8-BOM
有人知道如何保持与我保存回来的格式相同。
$myfile = "C:\hrfeed\output\$file"
$stringToXML.save($myfile)
谢谢
[如果您想将XML另存为UTF-8而没有BOM,并且使用Unix样式的换行符\n
而不是\r\n
,则不能在Windows上使用标准的Save()
方法,需要自己创建一个函数来执行那。
以您的previous question为例,您可以这样做:
[xml]$xmldata = @"
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Identity PUBLIC "point.dtd" "point.dtd"[]>
<Identity created="1525465321820" name="Onboarding - GUI - External">
<Attributes>
<Map>
<entry key="displayName" value="Onboarding - GUI " />
<entry key="firstname" value="Z Orphaned ID" />
</Map>
</Attributes>
</Identity>
"@
# do something with the xml data
要使用UNIX样式换行符以及以UTF-8 No BOM编码将xml保存到文件,可以使用此功能:
function Out-UnixXml {
[CmdletBinding()]
param(
[Parameter(ValueFromPipeline = $true, Mandatory = $true, Position = 0)]
[xml]$xml,
[Parameter(ValueFromPipeline = $true, Mandatory = $true, Position = 1)]
[Alias('FilePath')]
[string]$Path
)
try {
$settings = [System.Xml.XmlWriterSettings]::new()
$settings.Indent = $true # defaults to $false
$settings.NewLineChars = "`n" # defaults to "`r`n"
$settings.Encoding = [System.Text.UTF8Encoding]::new($false) # $false means No BOM
$xmlWriter = [System.Xml.XmlWriter]::Create($Path, $settings)
$xml.WriteTo($xmlWriter)
$xmlWriter.Flush()
}
finally {
# cleanup
if ($xmlWriter) { $xmlWriter.Dispose() }
}
}
并像这样使用它而不是$xmldata.Save('C:\somefile.xml')
Out-UnixXml $xmldata 'C:\somefile.xml'
关于DOCTYPE声明中的方括号。参见XmlDocument.Save() inserts empty square brackets in doctype declaration