【问题标题】:Delete multiline commets in xml - powershell删除xml中的多行注释-powershell
【发布时间】:2023-01-16 20:38:54
【问题描述】:

我想在没有 cmets 的情况下获取内容并部分成功,但是多行注释很糟糕。 源代码: <add key="Service.Cat" value="xxxxxx" provider-name="SQL" date-format="yyyy-MM-dd HH:mm:ss"> <counters /> </add> <!-- <add key="XXXX" value="SQL;Persist Security Info=False;User ID=xxxxx;Password=;Initial Catalog=xxxxx;Data Source=xxxxxx" provider-name="SQL" date-format="yyyy-MM-dd HH:mm:ss"> <counters /> </add> <add key="LB" value="SQL;Initial Catalog=FTU;Data Source=xxx.xxx.xxx;Integrated Security=xxxx" provider-name="SQL" date-format="yyyy-MM-dd HH:mm:ss"> <counters /> </add>--> <add key="xxxxxx" value="Initial Catalog=xzxxx;Data Source=xxxxxx;Trusted_Connection=yes;" provider-name="SQL" date-format="yyyy-MM-dd HH:mm:ss"> <counters />

电源外壳代码: Get-Content -Encoding UTF8 -Path 'C:\config1.xml'| % {$_ -replace ("<!--([\s\S]+?)-->","") } | out-file C:\config_temp.xml

但是评论仍然存在,而一行通常被删除。

电源外壳代码: Get-Content -Encoding UTF8 -Path 'C:\config1.xml'| % {$_ -replace ("<!--([\s\S]+?)-->","") } | out-file C:\config_temp.xml

但是评论仍然存在,而一行通常被删除。

【问题讨论】:

  • 不要使用正则表达式处理 XML,这是一个大错——而且没有必要,PowerShell 具有完整的 XML 支持。例如。 $x = [xml] (Get-Content 'myfile'); $comments = $x.SelectNodes('//comment()'); foreach ($comment in $comments) { $comment.ParentNode.RemoveChild($comment) | Out-Null }; Set-Content 'newfile' $x.InnerXml

标签: xml windows powershell


【解决方案1】:

您不应将 xml 视为普通文本,而应为此使用 PowerShell 的 xml 功能:

如果您的 xml 文件看起来像这样

<config>
    <add key="Service.Cat" value="xxxxxx" provider-name="SQL" date-format="yyyy-MM-dd HH:mm:ss">
        <counters />
    </add>
    <!--    
        <add key="XXXX" value="SQL;Persist Security Info=False;User ID=xxxxx;Password=;Initial Catalog=xxxxx;Data Source=xxxxxx" provider-name="SQL" date-format="yyyy-MM-dd HH:mm:ss">
            <counters /></add><add key="LB" value="SQL;Initial Catalog=FTU;Data Source=xxx.xxx.xxx;Integrated Security=xxxx" provider-name="SQL" date-format="yyyy-MM-dd HH:mm:ss"><counters />
        </add>
    -->
    <add key="xxxxxx" value="Initial Catalog=xzxxx;Data Source=xxxxxx;Trusted_Connection=yes;" provider-name="SQL" date-format="yyyy-MM-dd HH:mm:ss">
        <counters />
    </add>
    <!-- This is a single line comment -->
</config>

你想删除全部cmets,这样做:

# load the xml file. This way, you are ensured to get the file encoding correct
$xml = [System.Xml.XmlDocument]::new()
$xml.Load('X:FullPathToTheFile.xml')

$commentNodes = $xml.SelectNodes("//comment()")
foreach ($node in $commentNodes) {
    [void]$node.ParentNode.RemoveChild($node)
}
$xml.Save('X:FullPathToTheFile.xml')

如果你想删除只有多线cmets 并保持单行 cmets 不变,执行以下操作:

# load the xml file. This way, you are ensured to get the file encoding correct
$xml = [System.Xml.XmlDocument]::new()
$xml.Load('X:FullPathToTheFile.xml')

$commentNodes = $xml.SelectNodes("//comment()")
foreach ($node in $commentNodes) {
    if ($node.InnerText -match '
?
') {
        [void]$node.ParentNode.RemoveChild($node)
    }
}
$xml.Save('X:FullPathToTheFile.xml')

【讨论】:

    猜你喜欢
    • 2023-02-01
    • 2012-10-15
    • 1970-01-01
    • 2010-09-28
    • 2013-03-26
    • 2012-06-12
    • 2014-02-03
    • 2016-05-21
    • 2020-07-14
    相关资源
    最近更新 更多