过去几天我一直在尝试创建一个PowerShell脚本,该脚本将创建一个带有2个字段的XML文件。问题是创建的XML是CR LF with UTF-8 BOM
,我需要的是LF with UTF-8
。
edit - 遵循'lotpings'的建议,添加代码确实将XML更改为unix(fl),但它仍然在UTF8 -BOM上,而这会导致我问题。如何将其从UTF8-BOM更改为UTF8?
我从未处理过PowerShell,我不知道如何实现这一目标。
# Set The Formatting
$xmlsettings = New-Object System.Xml.XmlWriterSettings
$xmlsettings.Indent = $true
$xmlsettings.IndentChars = " "
$hour = (Get-Date).hour
$day = (Get-Date).dayofweek
# Checking If Its Too Late
If ($day -eq "Saturday" -and $hour -lt "8")
{
$hour = "00"
}
else
{
if ($day -ne "Saturday" -and $hour -lt "7")
{
$hour = "00"
}
}
# Set the File Name Create The Document
$XmlWriter = [System.XML.XmlWriter]::Create("S:NowPlayingCh0-News13.xml", $xmlsettings)
# Start the Root Element
$xmlWriter.WriteStartElement("track")
$xmlWriter.WriteElementString("name",$hour.ToString()+":00" + "News Flash of")
$xmlWriter.WriteElementString("artist","News Channel")
$xmlWriter.WriteEndElement() # <-- End <Track>
# End, Finalize and close the XML Document
$xmlWriter.WriteEndDocument()
$xmlWriter.Flush()
$xmlWriter.Close()
用:
展开您的$xmlsettings
$xmlsettings.NewLineChars = "`n"
样本输出(HEX)0a标记为[0A]
:
> Format-Hex .Ch0-News13.xml
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
00000000 EF BB BF 3C 3F 78 6D 6C 20 76 65 72 73 69 6F 6E <?xml version
00000010 3D 22 31 2E 30 22 20 65 6E 63 6F 64 69 6E 67 3D ="1.0" encoding=
00000020 22 75 74 66 2D 38 22 3F 3E[0A]3C 74 72 61 63 6B "utf-8"?>.<track
00000030 3E[0A]20 20 20 20 3C 6E 61 6D 65 3E 31 36 3A 30 >. <name>16:0
00000040 30 4E 65 77 73 20 46 6C 61 73 68 20 6F 66 3C 2F 0News Flash of</
00000050 6E 61 6D 65 3E[0A]20 20 20 20 3C 61 72 74 69 73 name>. <artis
00000060 74 3E 4E 65 77 73 20 43 68 61 6E 6E 65 6C 3C 2F t>News Channel</
00000070 61 72 74 69 73 74 3E[0A]3C 2F 74 72 61 63 6B 3E artist>.</track>
## Q:Test2019 221SO_54809424.ps1
# Set The Formatting
$File = "S:NowPlayingCh0-News13.xml"
$xmlsettings = New-Object System.Xml.XmlWriterSettings
$xmlsettings.Indent = $true
$xmlsettings.IndentChars = " "
$xmlsettings.NewLineChars = "`n"
$hour = (Get-Date).hour
$day = (Get-Date).dayofweek
# Checking If Its Too Late
If ($day -eq "Saturday" -and $hour -lt "8"){
$hour = "00"
} else {
if ($day -ne "Saturday" -and $hour -lt "7"){
$hour = "00"
}
}
# Set the File Name Create The Document
$XmlWriter = [System.XML.XmlWriter]::Create($File, $xmlsettings)
# Start the Root Element
$xmlWriter.WriteStartElement("track")
$xmlWriter.WriteElementString("name",$hour.ToString()+":00" + "News Flash of")
$xmlWriter.WriteElementString("artist","News Channel")
$xmlWriter.WriteEndElement() # <-- End <Track>
# End, Finalize and close the XML Document
$xmlWriter.WriteEndDocument()
$xmlWriter.Flush()
$xmlWriter.Close()