【问题标题】:XML path contains quotation marks, use PowerShell script Change values and save to original file [duplicate]XML路径包含引号,使用PowerShell脚本更改值并保存到原始文件[重复]
【发布时间】:2021-03-17 15:40:47
【问题描述】:

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

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

例如: 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
  1. 保存到原始文件

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

文件.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>

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

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

【问题讨论】:

  • PowerShell 将 XML、JSON 视为一等公民,并且有 Powershell XML cmdlet。 Powershell 本身在其基础上使用了大量的 XML、JSON,只需查找并使用它们。 MS powershellgallery.com 中也有用于 XML 和 JSON 用例的模块。 PowerShell 是基于 .Net 的,因此您可以使用 .Net XML 命名空间来处理 XML 文件。 PowerShell 上有许多端到端的 Youtube 视频。所以,搜索'PowerShell parsing XML'
  • 至于这个……【我没有搜索过PowerShell的相关脚本】,为什么不呢?这是一个简单的搜索工作。 There are tons of examples of parsing XML using PowerShell.

标签: regex xml powershell


【解决方案1】:

始终从内置帮助文件和这些帮助文件中的示例开始。

PowerShell 为 Microsoft powershellgallery.com 中的 XML 和其他模块提供特定的 cmdlet。

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

来自内置帮助文件

# 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

【讨论】:

  • 获取帮助获取帮助-完整
  • 替换方法不起作用,因为我的真实文档有很多相同的字段
  • 好的,取消了,但是再一次,考虑到您说您根本没有花时间查找 PowerShell 信息或审查 PowerShell,我只是想提供一些启发或方法,您可能能够利用其他基于字符串的用例。我只是使用 XML,因为它就在这里。我完全理解,这样的文件可能会很长。然而,如果字符串始终完全相同,它们将全部被替换/替换。
  • 这是一连串的信息,与 OP 的具体问题只有非常松散的联系。
猜你喜欢
  • 1970-01-01
  • 2022-01-01
  • 2014-05-10
  • 1970-01-01
  • 2018-05-28
  • 1970-01-01
  • 2010-12-15
  • 1970-01-01
  • 2011-08-27
相关资源
最近更新 更多