【问题标题】:Failing to Create XML in powershell with LF & UTF-8使用 LF 和 UTF-8 在 powershell 中创建 XML 失败
【发布时间】:2019-07-15 12:07:50
【问题描述】:

过去几天我一直在尝试创建一个 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()

【问题讨论】:

标签: xml powershell lf


【解决方案1】:

扩展您的$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()

【讨论】:

  • 所以我已经将该行添加到我的 .ps1 文件中,但我仍然遇到同样的问题。我创建的 XML 仍然是(Windows CR LF)和 UTF-8 BOM。
  • 我刚刚再次检查,我更改的脚本(附加在上面)产生了完全显示的输出。
猜你喜欢
  • 2020-09-24
  • 2015-02-15
  • 2010-10-19
  • 1970-01-01
  • 2018-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多