0

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

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

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

tianyi
  • 357
  • 4
  • 15
  • PowerShell treats XML, JSON as first-class citizens and there are Powershell XML cmdlets. Powershell itself uses tons of XML, JSON at its base, just look this up and use them. There are modules in the MS powershellgallery.com for XML and JSON use cases as well. PowerShell is .Net-based, so you can use the .Net XML namespace to deal with XML files. There are many Youtube videos on PowerShell end to end. So, search the for ['PowerShell parsing XML'](https://www.youtube.com/results?search_query=powershell+parsing+xml) – postanote Dec 05 '20 at 23:23
  • As for this... [I have not searched for the relevant scripts of PowerShell], why not? It's a simple search effort. [There are tons of examples of parsing XML using PowerShell](https://duckduckgo.com/?q=%27powershell+parsing+xml%27&t=h_&ia=web). – postanote Dec 05 '20 at 23:31

1 Answers1

0

Always start with the built-in help files and the examples from those help files.

PowerShell provides specific cmdlets for XML and other modules exist in the Microsoft powershellgallery.com.

Get-Command -Name '*xml*'
# Results
<#
CommandType     Name                                               Version    Source                                                                         
-----------     ----                                               -------    ------                                                                         
...                                                                  
Function        ConvertFrom-PSFClixml                              1.4.150    PSFramework                                                                    
Function        ConvertFrom-PSFClixml                              1.1.59     PSFramework                                                                    
Function        ConvertFrom-PSFClixml                              1.0.12     PSFramework                                                                    
Function        ConvertFrom-PSFClixml                              1.0.2      PSFramework                                                                    
...                                                                        
Function        ConvertTo-PSFClixml                                1.4.150    PSFramework                                                                    
Function        ConvertTo-PSFClixml                                1.1.59     PSFramework                                                                    
Function        ConvertTo-PSFClixml                                1.0.12     PSFramework                                                                    
Function        ConvertTo-PSFClixml                                1.0.2      PSFramework                                                                    
...                                                                         
Function        Export-PSFClixml                                   1.4.150    PSFramework                                                                    
Function        Export-PSFClixml                                   1.1.59     PSFramework                                                                    
Function        Export-PSFClixml                                   1.0.12     PSFramework                                                                    
Function        Export-PSFClixml                                   1.0.2      PSFramework                                                                    
...                                                                        
Function        Import-PSFClixml                                   1.4.150    PSFramework                                                                    
Function        Import-PSFClixml                                   1.1.59     PSFramework                                                                    
Function        Import-PSFClixml                                   1.0.12     PSFramework                                                                    
Function        Import-PSFClixml                                   1.0.2      PSFramework                                                                    
...                                                              
Cmdlet          ConvertTo-Xml                                      3.1.0.0    Microsoft.PowerShell.Utility                                                   
...                                                             
Cmdlet          Export-Clixml                                      3.1.0.0    Microsoft.PowerShell.Utility                                                   
Cmdlet          Import-Clixml                                      3.1.0.0    Microsoft.PowerShell.Utility                                                   
...                                                              
Cmdlet          Select-Xml                                         3.1.0.0    Microsoft.PowerShell.Utility  
#>

From the built-in help files

# Get specifics for a module, cmdlet, or function
(Get-Command -Name Import-Clixml ).Parameters
(Get-Command -Name Import-Clixml ).Parameters.Keys
Get-help -Name Import-Clixml  -Examples
# Results
<#
Get-Process | Export-Clixml pi.xml
$Processes = Import-Clixml pi.xml
$Credxmlpath = Join-Path (Split-Path $Profile) TestScript.ps1.credential
$Credential | Export-CliXml $Credxmlpath
$Credxmlpath = Join-Path (Split-Path $Profile) TestScript.ps1.credential
$Credential = Import-CliXml $Credxmlpath
#>
Get-help -Name Import-Clixml  -Full
Get-help -Name Import-Clixml  -Online

(Get-Command -Name Select-Xml).Parameters
(Get-Command -Name Select-Xml).Parameters.Keys
Get-help -Name Select-Xml -Examples
# Results
<#
 $Path = "$Pshome\Types.ps1xml"
$XPath = "/Types/Type/Members/AliasProperty"
Select-Xml -Path $Path -XPath $Xpath | Select-Object -ExpandProperty Node
[xml]$Types = Get-Content $Pshome\Types.ps1xml
Select-Xml -Xml $Types -XPath "//MethodName"
$Namespace = @{command = "http://schemas.microsoft.com/maml/dev/command/2004/10"; maml = "http://schemas.microsoft.com/maml/2004/10"; dev = 
$Path = "$Pshome\en-us\*dll-Help.xml"
$Xml = Select-Xml -Path $Path -Namespace $Namespace -XPath "//command:name"
$Xml | Format-Table @{Label="Name"; Expression= {($_.node.innerxml).trim()}}, Path -AutoSize
$Xml = @"
Select-Xml -Content $Xml -XPath "//edition" | foreach {$_.node.InnerXML}
$Xml | Select-Xml -XPath "//edition" | foreach {$_.node.InnerXML}
$SnippetNamespace = @{snip = "http://schemas.microsoft.com/PowerShell/Snippets"}
Select-Xml -Path $Home\Documents\WindowsPowerShell\Snippets -Namespace $SnippetNamespace -XPath "//snip:Title" | foreach {$_.Node.Innerxml}
#>
Get-help -Name Select-Xml -Full
Get-help -Name Select-Xml -Online
postanote
  • 15,138
  • 2
  • 14
  • 25
  • Get-Help Get-Help -Full – Doug Maurer Dec 06 '20 at 00:41
  • The substitution method doesn't work because my real document has many of the same fields – tianyi Dec 06 '20 at 00:59
  • 1
    OK, pulled that, but again, considering you said you spent no time looking up PowerShell information or reviewing PowerShell in general at all, I was just trying to provide some edification or approaches you may be able to leverage in other string-based use cases. I just use you XML because, well, it was here. I fully understand, that such files, can be very long. Yet, if the string was exactly the same throughout, they woudl all get replace/substituted. – postanote Dec 06 '20 at 01:08
  • That's a flurry of information with only a very loose connection to the OP's specific problem. – mklement0 Dec 06 '20 at 03:17