【问题标题】:Modify the value of XML node use powershell修改xml节点使用powershell的值
【发布时间】:2020-12-06 11:39:40
【问题描述】:

我想用PowerShell脚本修改以下两个路径中的值,

使用以下代码获取值。如何修改它们并保存到原始文件中?

eg: 1.将下面路径的值改为5.0

([xml] (Get-Content -Raw file.xml)).Map.StyleGroup.RootTopicDefaultsGroup.DefaultSubTopicShape.RightMargin

2.将以下路径的值改为false

([xml] (Get-Content -Raw file.xml)).Map.Custom.UpdatedNamedView

3.保存到原文件

注意:使用替换方法不起作用,因为实际文档中有很多相同的字段

文件.xml:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ap:Map xmlns:ap="http://schemas.mindjet.com/MindManager/Application/2003" OId="pdhXhObhC0avKT9HfmeUMQ==" xmlns:pri="http://schemas.mindjet.com/MindManager/Primitive/2003" xmlns:cor="http://schemas.mindjet.com/MindManager/Core/2003" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://schemas.mindjet.com/MindManager/Application/2003 http://schemas.mindjet.com/MindManager/Application/2003 http://schemas.mindjet.com/MindManager/Core/2003 http://schemas.mindjet.com/MindManager/Core/2003 http://schemas.mindjet.com/MindManager/Delta/2003 http://schemas.mindjet.com/MindManager/Delta/2003 http://schemas.mindjet.com/MindManager/Primitive/2003 http://schemas.mindjet.com/MindManager/Primitive/2003">
  <cor:Custom Uri="http://schemas.mindjet.com/MindManager/UpdateCompatibility/2004" cst0:UpdatedCategories="true" Index="0" cst0:UpdatedNamedView="true" cst0:UpdatedTextLabelSetIds="true" cst0:UpdatedGanttViewProperties="true" cst0:UpdatedVisibilityStyle="true" cst0:UpdatedDuration="true" xmlns:cst0="http://schemas.mindjet.com/MindManager/UpdateCompatibility/2004"/>
  <ap:StyleGroup>
    <ap:RootTopicDefaultsGroup>
      <ap:DefaultSubTopicShape BottomMargin="3.5" SubTopicShape="urn:mindjet:RoundedRectangle" VerticalBottomMargin="2.5" RightMargin="3.5" LeftMargin="3.5" VerticalLeftMargin="2.5" VerticalRightMargin="2.5" VerticalTopMargin="2.5" TopMargin="3.5"/>
    </ap:RootTopicDefaultsGroup>
  </ap:StyleGroup>
</ap:Map>

真实文档file.xml:https://www.upload.ee/files/12607236/file.zip.html

相关文件下载:https://www39.zippyshare.com/v/0EoigKun/file.html

节点视频演示:https://www59.zippyshare.com/v/4EVyDtUX/file.html

【问题讨论】:

    标签: xml powershell


    【解决方案1】:
    # Get the full path of the input file.
    $filePath = Convert-Path file.xml
    
    # Parse the file into an XML DOM.
    [xml] $xml = Get-Content -Raw $filePath
    
    # Modify the attributes of interest.
    
    $xml.Map.StyleGroup.RootTopicDefaultsGroup.DefaultSubTopicShape.RightMargin = '5.0'
    
    $xml.Map.Custom.UpdatedNamedView = 'false'
    
    # Save the modified DOM back to the input file.
    # Note: Be sure to use a *full* path, because .NET's working dir
    #       usually differs from PowerShell's.
    $xml.Save($filePath)
    

    【讨论】:

    • @tianyi:XML 严格基于文本(基于字符串)。如果您要使用 PowerShell 布尔值 $true$false,它们将逐字逐句地字符串化(转换为字符串)TrueFalse - 请注意开头的大写字符。
    猜你喜欢
    • 1970-01-01
    • 2011-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-27
    • 2012-06-06
    相关资源
    最近更新 更多