![](/img/trans.png)
[英]How to remove special characters while saving xml string using Powershell
[英]How to Remove Special/Bad Characters from XML Using Powershell
以下正則表達式將通過指定一個否定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.