문제

XML을 UTF16에서 UTF8 인코딩 된 파일로 변환하는 가장 쉬운 방법은 무엇입니까?

도움이 되었습니까?

해결책

이것은 가장 최적이 아니지만 작동합니다. 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;
}

다른 팁

글쎄, 가장 쉬운 방법은 파일이 XML인지 아닌지에 대해 신경 쓰지 않는 것입니다.

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

이것은 없을 때만 XML에 대해서만 작동합니다.

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

선.

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)      
}

당신은보고 싶을 수도 있습니다 XMLDocument 자세한 내용은 CreateXmlDeclaration.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top