2015-03-31 45 views
1

以下XML字符串去除特殊字符是PowerShell的代碼,我使用從web.config文件中刪除父節點:如何同時節約使用PowerShell

$c = "C:\test\web.config" 
    $xml = (Get-Content $c) 
    $ConnectionString = $xml.configuration.appSettings.add | Where-Object {$_.Key -eq 'ConnectionString'} 
    $xml.configuration.RemoveChild($ConnectionString.ParentNode) 
    $xml.save($c) 

After removing <appSettings> tag from web.config file. Some special Characters like &#xA; are adding to web.config file as below: 

<?xml version="1.0"?> 
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0"> 
    <configSections> 
    <sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, &#xA;  System.Web.Extensions, Version=3.5.0.0, Culture=neutral, &#xA;  PublicKeyToken=31bf3856ad364e35"> 
    </configSections> 
</configuration> 

請讓我知道如何保存XML文件無特殊字符,如&#xA;和新線

回答

1

剛把這個問題,我過濾所有非ASCII-256字符與此正則表達式

$myText = # your text here 
$cleanText = ($myText -replace"[^\x00-\xFF]",""); 

你確定這應刪除字符?您也可以嘗試更改編碼,像這樣

$myText | Out-File -FilePath "yourPath" -Encoding utf8 
我已經使用
+0

都[^ \ x00- \ XFF]和xml文件還是一樣的特殊字符即 和 來了的白色,而不是UTF8編碼空間 – Praveen 2015-04-01 10:38:26