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

时间:2015-03-31 09:41:47

标签: 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($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 个答案:

答案 0 :(得分:1)

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

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

你确定要删除这些字符吗?您也可以尝试使用类似的

更改编码
$myText | Out-File -FilePath "yourPath" -Encoding utf8