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

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

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

相关标签:
3条回答
  • 2020-12-09 09:53

    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;
    }
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-09 10:08

    Well, I guess the easiest way is to just not care about whether the file is XML or not and simply convert:

    Get-Content file.foo -Encoding Unicode | Set-Content -Encoding UTF8 newfile.foo
    

    This will only work for XML when there is no

    <?xml version="1.0" encoding="UTF-16"?>
    

    line.

    0 讨论(0)
提交回复
热议问题