我应该使用XML ISO-8859-1的ANSI编码吗? [英] Should I use ANSI Encoding with XML ISO-8859-1?

查看:455
本文介绍了我应该使用XML ISO-8859-1的ANSI编码吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用PowerShell处理一个XML文件,该文件在其标题中指定使用ISO 8851-1编码。

I need to use PowerShell to process an XML file which specifies in its heading that it is encoded with ISO 8851-1.

需要什么:


处理后,XML必须仍然可以由生成它的windows
应用程序打开,因此处理必须仍然给出
XML符合ISO 8851-1。

After processing the XML must still be openable by the windows application which has generated it, so processing must still give an XML compatible with ISO 8851-1.

根据我的理解WindowsANSI是ISO 8859的扩展。PowerShell Get-Content Set-Content 有ANSI选项。

As I understand Windows "ANSI" is an extension of ISO 8859. PowerShell Get-Content and Set-Content have ANSI option. Did I understand well?

推荐答案

使用PowerShell处理XML文件时,应该读取以下文件:

When processing XML files with PowerShell you should read the files like this:

[xml]$xml = Get-Content 'C:\path\to\input.xml'

并保存为:

$xml.Save('C:\path\to\output.xml')

这应该自动照顾编码。如果没有,您可以使用 StreamWriter

That should automatically take care of the encoding. If not, you can enforce an encoding by using a StreamWriter:

$filename = 'C:\path\to\output.xml'
$encoding = [Text.Encoding]::GetEncoding('iso-8859-1')
$writer = New-Object IO.StreamWriter ($filename, $false, $encoding)
$xml.Save($writer)
$writer.Close()
$writer.Dispose()

这篇关于我应该使用XML ISO-8859-1的ANSI编码吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆