简体   繁体   中英

Powershell: how to select and modify Xml comment?

I've got some xml files like below:

<?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>

I wish to use powershell to changes these xml files: select the comment tags and change "my comments" into "modified".

How to do it using XPath or .net xml utility? Thanks a lot.

Using .net

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

another option using replace

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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