繁体   English   中英

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

[英]How to remove special characters while saving xml string using 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>

请让我知道如何保存不带&#xA;等特殊字符的xml文件&#xA; 和新线

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

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

您确定应该删除这些字符吗? 您也可以尝试通过类似这样的方式更改编码

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM