简体   繁体   English

On converting the UFT-8 xml to Unicode in Powershell, $encoding attribute value is showing bigEndianUnicode in the output xml, I want UTF-16 there

[英]On converting the UFT-8 xml to Unicode in Powershell, $encoding attribute value is showing bigEndianUnicode in the output xml, I want UTF-16 there

Getting this line in output file after converting UTF-8 to Unicode将 UTF-8 转换为 Unicode 后在 output 文件中获取此行

<?xml version="1.0" encoding="bigEndianUnicode"?>

But I need below line in the xml但我需要 xml 中的以下行

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

Assuming you're working with [xml] type, you can set encoding of a XML file as follows:假设您正在使用[xml]类型,您可以设置 XML 文件的编码如下:

[xml] $xmlData = '<example>XML</example>'

$fileName = 'C:\test.xml'

$settings = New-Object System.Xml.XmlWriterSettings

# Set encoding to UTF-16
$settings.Encoding = [System.Text.Encoding]::Unicode

$xmlWriter = [System.Xml.XmlWriter]::Create($fileName, $settings)

$xmlData.Save($xmlWriter)

$xmlWriter.Close()

Giorgi Chakhidze's helpful answer shows a proper, XML API -based way to produce an XML file with a given encoding that is also reflected in the output file's XML declaration. Giorgi Chakhidze's helpful answer shows a proper, XML API -based way to produce an XML file with a given encoding that is also reflected in the output file's XML declaration.

However, it sounds like you've used plain-text processing to transcode files from UTF-8 to "Unicode" (UTF-16LE), and must now adapt these files' XML declarations to match the new encoding.但是,听起来您已经使用纯文本处理将文件从 UTF-8转码为“Unicode”(UTF-16LE),现在必须调整这些文件的 XML 声明以匹配新编码。

The following shows a solution for a single file.xml file (it assumes that file.xml has a "Unicode" (UTF-16LE) BOM , so that Get-Content interprets its encoding correctly):下面显示了单个file.xml文件的解决方案(它假设file.xml具有“Unicode”(UTF-16LE) BOM ,以便Get-Content正确解释其编码):

(Get-Content -Raw -LiteralPath file.xml) -replace '(?<=^.+ encoding=")[^"]+', 'utf-16' |
  Set-Content -NoNewLine -Literal Path file.xml

However, it's unclear how your transcoded-from-UTF-8 files ever ended up with encoding="bigEndianUnicode" in their XML declaration.但是,尚不清楚您的从 UTF-8 转码的文件是如何在其 XML 声明中以encoding="bigEndianUnicode"结尾的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM