簡體   English   中英

如何使用 Powershell 從 XML 中刪除特殊/壞字符

[英]How to Remove Special/Bad Characters from XML Using Powershell

我有一個 XML 文件,我想從以下文件中刪除那些十六進制字符錯誤是無效字符:

在此處輸入圖像描述

我不知道 STX 是什么意思,當我嘗試將其復制到剪貼板並將其粘貼到 MS Work 中時,它顯示了一些其他值。

如何在 powershell 中編寫腳本以從我的 XML 文件中刪除上述內容。

以下正則表達式將通過指定一個否定XML文檔中整個有效unicode條目集的字符類來從XML中刪除所有無效字符:

$rPattern = "[^\x09\x0A\x0D\x20-\xD7FF\xE000-\xFFFD\x10000\x10FFFF]"
$xmlText -replace $rPattern,''

這可以很容易地變成一個簡單的函數

function Repair-XmlString
{
  [CmdletBinding()]
  param(
    [Parameter(Mandatory=$true,Position=0)]
    [string]$inXML
  )

  # Match all characters that does NOT belong in an XML document
  $rPattern = "[^\x09\x0A\x0D\x20-\xD7FF\xE000-\xFFFD\x10000\x10FFFF]"

  # Replace said characters with [String]::Empty and return
  return [System.Text.RegularExpressions.Regex]::Replace($inXML,$rPattern,"")
}

然后做:

Repair-XmlString (Get-Content path\to\file.xml -Raw) |Set-Content path\to\file.xml 

功能更新:)

function Repair-XmlString {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory = $true, Position = 0,ValueFromPipeline)]
        [ValidateNotNullOrEmpty()]
        [string]$String
    )
    Write-Host "Cleaning string for XML parsing [String: $($String)]"
    $rPattern = "[^\x09\x0A\x0D\x20-\xD7FF\xE000-\xFFFD\x10000\x10FFFF]"
    $cleaned = $String -replace $rPattern, ''
    Write-Host "Returning parsed string [String cleaned: $($cleaned)]"
    return $cleaned
}
# Another way, No 'Repair-XmlString' function needed, just wrap the string in single quotes
# This is an example of exporting install software names that have foreign characters. 

# Wrap output string with foreign characters in single quotes
$InstalledSoftware = Get-ChildItem HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\, HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\ |  Get-ItemProperty | select @{n="DisplayName";e={"\`'$($_.DisplayName)\`'"}}, DisplayVersion  

# Create Hastable
$SoftwareHashTable = @{SoftwareName = $InstalledSoftware }
# Output file path
$outFile = "$env:USERPROFILE\Desktop\HashFile.xml"   
# Export to hashtable xml                                                             
Export-Clixml -InputObject $SoftwareHashTable -Depth 4 -Path $outFile -Encoding UTF8 
# Import xml Hashtable
$ImportedXml = Import-Clixml $outFile    
# View values                                                                         
$ImportedXml.Values                                                                                               

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM