【问题标题】:Is there a way to convert a very large XML file with UTF8 encoding in powershell?有没有办法在powershell中使用UTF8编码转换一个非常大的XML文件?
【发布时间】:2020-01-15 14:02:23
【问题描述】:

我有一个大型 XML 文件 (1.7 GB),我正在尝试在导入 SQL 服务器之前对其应用 UTF-8 编码。使用powershell,转换时已经1个多小时了,系统完全没用,因为内存使用量最大。有人可以建议一种更有效的方式使用 powershell 来编码这个文件吗?那是因为我还有其他几个需要转换的文件。提前感谢您抽出宝贵时间回复。

这是目前采用的方法:

$sourcePath = "C:\Folder\myfile.xml"
$destinationPath = "E:\Folder\myfile.xml"
Write-Host "Converting $_"
$content = Get-Content $_.FullName
Set-content (Join-Path -Path $destinationPath -ChildPath $_) -Encoding UTF8 -Value $content

================================================ ==========================

【问题讨论】:

    标签: .net performance powershell encoding utf-8


    【解决方案1】:

    你试过了吗?

    [System.Io.File]::ReadAllText($sourcePath) | Out-File -FilePath $destinationPath -Encoding UTF8
    

    使用 .NET 类应该更快。

    【讨论】:

    • 我刚刚尝试过,现在的代码如下所示: $sourcePath = "C:\Folder\myfile.xml" $destinationPath = "E:\Folder\myfile.xml" Write-Host "转换 $_" $content = Get-Content $_.FullName Set-content (Join-Path -Path $destinationPath -ChildPath $_) -Encoding UTF8 -Value $content
    • 感谢您的回复。我刚试过,现在是: $sourcePath = "C:\Folder\myfile.xml" $destinationPath = "C:\Folder\myfile.xml" [System.Io.File]::ReadAllText($sourcePath) | Out-File -FilePath $destinationPath -Encoding UTF8 现在,我得到:使用“1”参数调用“ReadAllText”的异常:“拒绝访问路径'C:\Folder\'。”在 line:19 char:2 + [System.Io.File]::ReadAllText($sourcePath) | Out-File -FilePath $des + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : UnauthorizedAccessException
    猜你喜欢
    • 2012-07-15
    • 2020-07-12
    • 2013-07-15
    • 2011-11-19
    • 2018-06-14
    • 2019-03-31
    • 2020-02-24
    • 1970-01-01
    • 2013-06-04
    相关资源
    最近更新 更多