Last active
June 19, 2018 19:26
-
-
Save bandit145/cb85f99dee2b1a742327a68925a3d618 to your computer and use it in GitHub Desktop.
swapping powershell xml contexts between nodes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<bookstore> | |
<book category="cooking"> | |
<title lang="en">Everyday Italian</title> | |
<author>Giada De Laurentiis</author> | |
<year>2005</year> | |
<price>30.00</price> | |
</book> | |
<book category="children"> | |
<title lang="en">Harry Potter</title> | |
<author>J K. Rowling</author> | |
<year>2005</year> | |
<price>29.99</price> | |
</book> | |
<book category="web"> | |
<title lang="en">Learning XML</title> | |
<author>Erik T. Ray</author> | |
<year>2003</year> | |
<price>39.95</price> | |
</book> | |
</bookstore> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<bookstore> | |
<book category="cooking"> | |
<title lang="en">Everyday Italian</title> | |
<author>Giada De Laurentiis</author> | |
<year>2005</year> | |
<price>30.00</price> | |
</book> | |
</bookstore> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#put whatever here | |
[xml]$good_xml = Get-Content E:\downloads\Books.xml | |
[xml]$part_xml = Get-Content E:\downloads\BooksPartial.xml | |
$book = $good_xml.bookstore.book[1] | |
#swap contexts | |
#Create a new xmlobj under the context of the other object | |
$new_book = $part_xml.ImportNode($book,$true) | |
#verify new object is indeed under the right object | |
$new_book.OwnerDocument -eq $part_xml | |
#append | |
$part_xml.bookstore.AppendChild($new_book) | Out-Null | |
$part_xml.bookstore.book[1] | |
$part_xml.Save("E:\downloads\BooksPartial.xml") | |
#delete old book | |
$good_xml.bookstore.RemoveChild($book) | |
$good_xml.Save("E:\downloads\Books.xml") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment