如何使用PowerShell/XPath修改XML并保存文档?

时间:2021-05-19 07:15:29

I'm looking to use PowerShell to alter XML. I haven't been able to copy XML using XPath. I can load the XML, but I can't figure out how to list the XML with an XPath and create another XML file with what I retrieve.

我希望使用PowerShell修改XML。我还不能使用XPath复制XML。我可以加载XML,但是我不知道如何使用XPath列出XML并使用检索的内容创建另一个XML文件。

$doc = new-object "System.Xml.XmlDocument"
$doc.Load("XmlPath.xml")

Also, how would you add the removed XML to another XML file?

另外,如何将删除的XML添加到另一个XML文件中?

2 个解决方案

#1


15  

If you're using PowerShell 2.0 you can use the new Select-Xml cmdlet to select xml based on an XPath expression e.g.:

如果您正在使用PowerShell 2.0,您可以使用新的select - xml cmdlet根据XPath表达式选择xml,例如:

$xml = '<doc><books><book title="foo"/></books></doc>'
$xml | Select-Xml '//book'
Node    Path          Pattern
----    ----          -------
book    InputStream   //book

To remove nodes:

删除节点:

PS> $xml =[xml]'<doc><books><book title="foo"/><book title="bar"/></books></doc>'
PS> $xml | Select-Xml -XPath '//book' | 
        Foreach {$_.Node.ParentNode.RemoveChild($_.Node)}

title
-----
foo
bar

PS> $xml.OuterXml
<doc><books></books></doc>

Then to save to file:

然后保存到文件中:

$xml.Save("$pwd\foo.xml")
Get-Content foo.xml
<doc>
  <books>
  </books>
</doc>

#2


1  

Load Linq Xml assemblies:

负载Linq Xml组件:

[System.Reflection.Assembly]::LoadWithPartialName("System.Xml.Linq")
[System.Reflection.Assembly]::LoadWithPartialName("System.Xml.XPath")

Load your xml (Note, you can use ::Load("file") instead of ::Parse(...) to load from file:

加载xml(注意,可以使用::Load(“file”)而不是::Parse(…)从file中加载:

$xml = [System.Xml.Linq.XDocument]::Parse("<root> <row>Hey</row> <row>you</row> </root>")

Modify (in this case Remove the first row:

修改(在本例中删除第一行:

[System.Xml.XPath.Extensions]::XPathSelectElement($xml, "//row").Remove()

Save to file:

保存到文件:

$xml.Save("MyXml.xml")

Using System.Xml (instead of System.Xml.Linq):

使用系统。Xml(而不是System.Xml.Linq):

$doc = new-object "System.Xml.XmlDocument"
$doc.Load("MyXml_int.xml")

$node = $doc.SelectSingleNode("//row");
$node.ParentNode.RemoveChild($node)

$doc.Save("MyXml_out.xml")

#1


15  

If you're using PowerShell 2.0 you can use the new Select-Xml cmdlet to select xml based on an XPath expression e.g.:

如果您正在使用PowerShell 2.0,您可以使用新的select - xml cmdlet根据XPath表达式选择xml,例如:

$xml = '<doc><books><book title="foo"/></books></doc>'
$xml | Select-Xml '//book'
Node    Path          Pattern
----    ----          -------
book    InputStream   //book

To remove nodes:

删除节点:

PS> $xml =[xml]'<doc><books><book title="foo"/><book title="bar"/></books></doc>'
PS> $xml | Select-Xml -XPath '//book' | 
        Foreach {$_.Node.ParentNode.RemoveChild($_.Node)}

title
-----
foo
bar

PS> $xml.OuterXml
<doc><books></books></doc>

Then to save to file:

然后保存到文件中:

$xml.Save("$pwd\foo.xml")
Get-Content foo.xml
<doc>
  <books>
  </books>
</doc>

#2


1  

Load Linq Xml assemblies:

负载Linq Xml组件:

[System.Reflection.Assembly]::LoadWithPartialName("System.Xml.Linq")
[System.Reflection.Assembly]::LoadWithPartialName("System.Xml.XPath")

Load your xml (Note, you can use ::Load("file") instead of ::Parse(...) to load from file:

加载xml(注意,可以使用::Load(“file”)而不是::Parse(…)从file中加载:

$xml = [System.Xml.Linq.XDocument]::Parse("<root> <row>Hey</row> <row>you</row> </root>")

Modify (in this case Remove the first row:

修改(在本例中删除第一行:

[System.Xml.XPath.Extensions]::XPathSelectElement($xml, "//row").Remove()

Save to file:

保存到文件:

$xml.Save("MyXml.xml")

Using System.Xml (instead of System.Xml.Linq):

使用系统。Xml(而不是System.Xml.Linq):

$doc = new-object "System.Xml.XmlDocument"
$doc.Load("MyXml_int.xml")

$node = $doc.SelectSingleNode("//row");
$node.ParentNode.RemoveChild($node)

$doc.Save("MyXml_out.xml")