如何在使用Powershell保存xml字符串时删除特殊字符

如何在使用Powershell保存xml字符串时删除特殊字符,xml,powershell,powershell-ise,cmdlet,Xml,Powershell,Powershell Ise,Cmdlet,以下是我用于从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($Connectio

以下是我用于从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>
$c=“c:\test\web.config”
$xml=(获取内容$c)
$ConnectionString=$xml.configuration.appSettings.add | Where对象{$\.Key-eq'ConnectionString'}
$xml.configuration.RemoveChild($ConnectionString.ParentNode)
$xml.save($c)
从web.config文件中删除标记后。一些特殊字符,如
;正在添加到web.config文件,如下所示:

请告诉我如何保存没有特殊字符的xml文件,如

和新行刚刚出现这个问题,我用这个正则表达式过滤了所有非ascii-256字符

$myText = # your text here
$cleanText = ($myText -replace"[^\x00-\xFF]","");
您确定应该删除此字符吗?您也可以尝试用类似这样的方式更改编码

$myText | Out-File -FilePath "yourPath" -Encoding utf8

我对xml文件使用了[^\x00-\xFF]和utf8编码,但仍然使用相同的特殊字符,即&xD ;和 ;而不是空白