Converting xml from UTF-16 to UTF-8 using PowerShell

后端 未结 3 966
旧巷少年郎
旧巷少年郎 2020-12-09 09:50

What\'s the easiest way to convert XML from UTF16 to a UTF8 encoded file?

3条回答
  •  天命终不由人
    2020-12-09 10:05

    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.

提交回复
热议问题