【问题标题】:How to remove special characters while saving xml string using Powershell如何在使用 Powershell 保存 xml 字符串时删除特殊字符
【发布时间】:2015-03-31 09:41:46
【问题描述】:

以下是我用来从 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>

请告诉我如何保存不带&amp;#xA; 和换行等特殊字符的xml 文件

【问题讨论】:

    标签: xml powershell powershell-ise cmdlet


    【解决方案1】:

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

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

    您确定要删除这些字符吗?您也可以尝试使用类似的方式更改编码

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

    【讨论】:

    • 我对 xml 文件使用了 [^\x00-\xFF] 和 utf8 编码仍然相同的特殊字符,即 和 即将到来而不是空白
    猜你喜欢
    • 2018-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-24
    相关资源
    最近更新 更多