What's the easiest way to convert XML from UTF16 to a UTF8 encoded file?
Converting xml from UTF-16 to UTF-8 using PowerShell
26k views Asked by David Gardiner At
3
There are 3 answers
2
On
Try this solution that uses a XmlWriter:
$encoding="UTF-8" # most encoding should work
$files = get-ChildItem "*.xml"
foreach ( $file in $files )
{
[xml] $xmlDoc = get-content $file
$xmlDoc.xml = $($xmlDoc.CreateXmlDeclaration("1.0",$encoding,"")).Value
$xmlDoc.save($file.FullName)
}
You may want to look at XMLDocument for more explanation on CreateXmlDeclaration.
This may not be the most optimal, but it works. Simply load the xml and push it back out to a file. the xml heading is lost though, so this has to be re-added.