Dav*_*ner 18 xml powershell utf-8 utf-16
将XML从UTF16转换为UTF8编码文件的最简单方法是什么?
Ben*_*aan 14
这可能不是最优的,但它有效.只需加载xml并将其推回到文件中.虽然xml标题丢失了,所以必须重新添加.
$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;
}
Run Code Online (Sandbox Code Playgroud)
Joe*_*oey 14
好吧,我想最简单的方法是不关心文件是否是XML,只需转换:
Get-Content file.foo -Encoding Unicode | Set-Content -Encoding UTF8 newfile.foo
Run Code Online (Sandbox Code Playgroud)
这只适用于没有的XML
<?xml version="1.0" encoding="UTF-16"?>
Run Code Online (Sandbox Code Playgroud)
线.
试试这个使用的解决方案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)
}
Run Code Online (Sandbox Code Playgroud)
您可能需要查看XMLDocument更多解释CreateXmlDeclaration.
| 归档时间: |
|
| 查看次数: |
18275 次 |
| 最近记录: |