What's the easiest way to convert XML from UTF16 to a UTF8 encoded file?
+6
A:
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.
$files = get-ChildItem "*.xml"
foreach ( $file in $files )
{
[System.Xml.XmlDocument]$doc = new-object System.Xml.XmlDocument;
$doc.set_PreserveWhiteSpace( $true );
$doc.Load( $file );
$root = $doc.get_DocumentElement();
$xml = $root.get_outerXml();
$xml = "<?xml version=`"1.0`" encoding=`"utf-8`"?>" + $xml
$newFile = $file.Name + ".new"
Set-Content -Encoding UTF8 $newFile $xml;
}
Ben Laan
2009-04-15 05:49:54
Shouldn't you explicitly set the encoding to save somewhere?
Joey
2009-04-15 05:52:28
If I knew how, I would. It appears to be the default though.
Ben Laan
2009-04-15 06:24:29
-Encoding parameter.
JasonMArcher
2009-04-16 01:37:17