【问题标题】:Powershell: how to select and modify Xml comment?Powershell:如何选择和修改 Xml 注释?
【发布时间】:2015-10-05 09:41:25
【问题描述】:

我有一些 xml 文件,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<Project>
  <ItemGroup>
    <Book Name="Learn Powershell" />
    <Book Name=".net Programming" />
    <Book Name="C# Programming" />
  </ItemGroup>
  <!--
    my comments
    -->
</Project>

我希望使用 powershell 来更改这些 xml 文件:选择注释标签并将“my cmets”更改为“modified”。

如何使用 XPath 或 .net xml 实用程序来做到这一点? 非常感谢。

【问题讨论】:

    标签: xml powershell xpath comments


    【解决方案1】:

    使用 .net

    [xml]$x=get-content file.xml 
    $x.Project."#comment"="modified"          
    $x.Save("c:\newxml.xml")   
    

    另一个使用替换的选项

    (get-content file.xml).Replace("my comments","modified") |out-file newxml.xml 
    

    【讨论】:

      猜你喜欢
      • 2019-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-16
      • 1970-01-01
      相关资源
      最近更新 更多