Modify the value of XML node use powershell

Deadly 提交于 2021-01-29 08:26:58

问题


I want to use the PowerShell script to modify the values in the following two paths,

Use the following code to get the values. How to modify them and save them to the original file?

eg: 1.Change the value of the following path to 5.0

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

2.Change the value of the following path to false

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

3.save to the original file

note: use The replacement method doesn't work because there are many of the same fields in the actual document

file.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>

Real document file.xml: https://www.upload.ee/files/12607236/file.zip.html

Related file download:https://www39.zippyshare.com/v/0EoigKun/file.html

Node video demonstration:https://www59.zippyshare.com/v/4EVyDtUX/file.html


回答1:


  • Parse your input file into an [xml] (System.Xml.XmlDocument) instance and save that instance.

  • Modify the elements of that instance using dot notation, via PowerShell's adaptation of the XML DOM.

  • Use the System.Xml.XmlDocument.Save method of the [xml] instance to save the modified DOM to a (the original) file.

# 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)


来源:https://stackoverflow.com/questions/65163702/modify-the-value-of-xml-node-use-powershell

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!