xml - 使用 LF 和 UTF-8 在 powershell 中创建 XML 失败
问题描述
过去几天我一直在尝试创建一个 Powershell 脚本,该脚本将创建一个 XML 文件,其中包含 2 个字段来表示时间。问题是创建的 XML 是CR LF with UTF-8 BOM
我需要的LF with UTF-8
。
编辑- 遵循“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:\NowPlaying\Ch0-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"
样本输出(十六进制)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:\Test\2019\02\21\SO_54809424.ps1
# Set The Formatting
$File = "S:\NowPlaying\Ch0-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()
推荐阅读
- python - 检查列表中的一项是否在列表列表中
- swift - 以编程方式将 onlick 事件添加到 NSView 而无需子类化
- c++ - 带有 if 语句的嵌套循环的时间复杂度
- c# - 尝试编写一个单元通过处理电子邮件的 SendGrid 测试一个天蓝色函数
- node.js - 如何为我的应用程序创建本地数据库?
- class - 从 Kotlin 中不为空的数据类中获取所有属性
- python - 从 s3 存储桶加载 XGBoost
- r - R ggplot 实际值和具有图例的同一图中的多个模拟点
- jestjs - jest coverageThreshold glob 排除了一些子文件夹
- javascript - 如何更新/删除 mongo db 中嵌入数组中的文档?