0

I've been trying the past few days to create a Powershell script that will create a XML file with 2 fields saying the time. Problem is the XML created is CR LF with UTF-8 BOM and what I need is LF with UTF-8.

Edit - Following 'LotPings' advise, adding the code at did change the XML to Unix(FL) but it still on UTF8-BOM while it causing me problems. how can i change it from UTF8-BOM to UTF8 ?

I have never handled Powershell and I have no idea how I can achieve that.

# 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()
Radi
  • 1
  • 1
  • 1
    Possible duplicate of [How can I generate XML with CR, instead of CRLF in XmlTextWriter](https://stackoverflow.com/questions/3415294/how-can-i-generate-xml-with-cr-instead-of-crlf-in-xmltextwriter) –  Feb 21 '19 at 15:24

1 Answers1

0

Expand your $xmlsettings with:

$xmlsettings.NewLineChars = "`n"

Sample output (hex) 0A marked with [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()
  • So i've added that line into my .ps1 file but i still have the same issue. the XML i've been created is still (Windows CR LF) and UTF-8 BOM. – Radi Feb 25 '19 at 08:48
  • I just checked again, my changed script (appended above) produces exactly the shown output. –  Feb 26 '19 at 17:26