{"id":603,"date":"2013-05-20T13:42:20","date_gmt":"2013-05-20T11:42:20","guid":{"rendered":"http:\/\/michlstechblog.info\/blog\/?p=603"},"modified":"2021-03-27T13:26:48","modified_gmt":"2021-03-27T12:26:48","slug":"powershell-some-basic-xml-handling-with-powershell-and-net","status":"publish","type":"post","link":"https:\/\/michlstechblog.info\/blog\/powershell-some-basic-xml-handling-with-powershell-and-net\/","title":{"rendered":"Powershell: Some basic XML handling with Powershell and .NET"},"content":{"rendered":"<div class=\"twoclick_social_bookmarks_post_603 social_share_privacy clearfix 1.6.4 locale-en_US sprite-en_US\"><\/div><div class=\"twoclick-js\"><script type=\"text\/javascript\">\/* <![CDATA[ *\/\njQuery(document).ready(function($){if($('.twoclick_social_bookmarks_post_603')){$('.twoclick_social_bookmarks_post_603').socialSharePrivacy({\"services\":{\"flattr\":{\"uid\":\"Michl\",\"status\":\"on\",\"the_title\":\"Powershell%3A%20Some%20basic%20XML%20handling%20with%20Powershell%20and%20.NET\",\"the_excerpt\":\"Hi%20everybody%2C%0D%0A%0D%0Ahere%20are%20some%20fundamentals%20to%20handle%20XML%20Files%20with%20powershell.%0D%0A%0D%0AThe%20following%20commands%20uses%20these%20XML%20File%0D%0A%0D%0A%5Bxml%5D%0D%0A%0D%0A%0D%0A%20%20%20%20%0D%0A%20%20%20%20%20%20%20%20C%3A%5Ctemp%5Cdoc0001.txt%0D%0A%20%20%20%20%20%20%20%20C%3A%5Ctemp%5Cdoc0002.txt%0D%0A%20%20%20%20%20%20%20%20C%3A%5Ctemp%5Cdoc0051.txt%0D%0A%20%20%20%20%20%20%20%20C%3A%5Ctemp%5Cdoc0041.txt%0D%0A%20%20%20%20%20%20%20%20C%3A%5Ctemp%5Cdoc0012.txt%0D%0A%20%20%20%20%20%20%20%20C%3A%5Ctemp%5Cdoc0003.txt%0D%0A%20%20%20%20%0D%0A%0D%0A%0D%0A%5B%2Fxml%5D%0D%0A%0D%0ACreating%C2%A0%20a%20XML%20File%2C%20create%20a%20document%20root%2C%20create%20su%20...\",\"txt_info\":\"2 clicks for more data protection:\\r\\n\\r\\nOnly when you click here, the button will be come active and you can send your recommendation to Flattr. When activating, data are transmitted to third parties. \",\"perma_option\":\"off\"}},\"txt_help\":\"When you activate these fields by clicking, information to Flattr may be transferred abroad, and probably may also stored there.\",\"settings_perma\":\"Enable permanently and accept data transmission. \",\"info_link\":\"http:\\\/\\\/www.heise.de\\\/ct\\\/artikel\\\/2-Klicks-fuer-mehr-Datenschutz-1333879.html\",\"uri\":\"https:\\\/\\\/michlstechblog.info\\\/blog\\\/powershell-some-basic-xml-handling-with-powershell-and-net\\\/\",\"post_id\":603,\"post_title_referrer_track\":\"Powershell%3A+Some+basic+XML+handling+with+Powershell+and+.NET\",\"display_infobox\":\"on\"});}});\n\/* ]]> *\/<\/script><\/div><p>Hi everybody,<\/p>\n<p>here are some fundamentals to handle XML Files with powershell.<\/p>\n<p>The following commands uses these XML File<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n\r\n&lt;config description=&quot;Config file for testing&quot;&gt;\r\n    &lt;system description=&quot;Document Management&quot;&gt;\r\n        &lt;document authorFirstName=&quot;Kirk&quot; authorSurname=&quot;Author1&quot; date=&quot;20130503 01:15:43&quot; description=&quot;Document Hammet&quot; index=&quot;3&quot;&gt;C:\\temp\\doc0001.txt&lt;\/document&gt;\r\n        &lt;document authorFirstName=&quot;Lars&quot; authorSurname=&quot;Author5&quot; date=&quot;20130503 10:05:42&quot; description=&quot;Document Ulrich&quot; index=&quot;4&quot;&gt;C:\\temp\\doc0002.txt&lt;\/document&gt;\r\n        &lt;document authorFirstName=&quot;Cliff&quot; authorSurname=&quot;Author5&quot; date=&quot;20130612 11:54:33&quot; description=&quot;Document Burton&quot;\u00a0 index=&quot;6&quot;&gt;C:\\temp\\doc0051.txt&lt;\/document&gt;\r\n        &lt;document authorFirstName=&quot;Jason&quot; authorSurname=&quot;Author2&quot; date=&quot;20130806 04:02:41&quot; description=&quot;Document Newsted&quot;\u00a0 index=&quot;1&quot;&gt;C:\\temp\\doc0041.txt&lt;\/document&gt;\r\n        &lt;document authorFirstName=&quot;Robert&quot; authorSurname=&quot;Smith&quot; date=&quot;20131202 07:12:03&quot; description=&quot;Document Trujillo&quot;\u00a0 index=&quot;2&quot;&gt;C:\\temp\\doc0012.txt&lt;\/document&gt;\r\n        &lt;document authorFirstName=&quot;James&quot; authorSurname=&quot;Smith&quot; date=&quot;20130211 09:05:59&quot; description=&quot;Document Hetfield&quot;\u00a0 index=&quot;5&quot;&gt;C:\\temp\\doc0003.txt&lt;\/document&gt;\r\n    &lt;\/system&gt;\r\n&lt;\/config&gt;\r\n\r\n<\/pre>\n<p>Creating\u00a0 a XML File, create a document root, create subelements, add and set attributes, save the file<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\n# Create a new XML File with config root node\r\n&#x5B;System.XML.XMLDocument]$oXMLDocument=New-Object System.XML.XMLDocument\r\n# New Node\r\n&#x5B;System.XML.XMLElement]$oXMLRoot=$oXMLDocument.CreateElement(\"config\")\r\n# Append as child to an existing node\r\n$oXMLDocument.appendChild($oXMLRoot)\r\n# Add a Attribute\r\n$oXMLRoot.SetAttribute(\"description\",\"Config file for testing\")\r\n&#x5B;System.XML.XMLElement]$oXMLSystem=$oXMLRoot.appendChild($oXMLDocument.CreateElement(\"system\"))\r\n$oXMLSystem.SetAttribute(\"description\",\"Document Management\")\r\n# Save File\r\n$oXMLDocument.Save(\"c:\\temp\\config.xml\")\r\n\r\n<\/pre>\n<p>Loading an existing XML File, read an element, do some XPATH queries<\/p>\n<p><!--more--><\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\n\r\n# Loading a XML File\r\n&#x5B;&#x5B;xml]]$oXMLDocument=Get-Content -Path \"c:\\temp\\config.xml\"\r\n# or\r\n$oXMLDocument=New-Object System.XML.XMLDocument\r\n$oXMLDocument.Load(\"c:\\temp\\config.xml\")\r\n# Accessing a node\/attribute\r\nwrite-host $oXMLDocument.config.description\r\n# Get all documents from Author \"Smith\"\r\n$oDocsSmith=$oXMLDocument.selectNodes(\"config\/system\/document&#x5B;@authorSurname=\"\"Smith\"\"]\")\r\n# Get all documents from Author \"Smith\" with date February 2013, XPATH Query relative to document root, http:\/\/msdn.microsoft.com\/en-US\/library\/ms256086.aspx\r\n$oDocsSmithFeb13=$oXMLDocument.selectNodes(\"config\/system\/document&#x5B;@authorSurname=\"\"Smith\"\" and starts-with(@date,\"\"201302\"\")]\")\r\n\r\n<\/pre>\n<p>Here are some examples<br \/>\nGet all Titles from a RSS Feed<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\n\r\n&#x5B;System.Net.WebClient]$oWebClient = New-Object System.Net.WebClient\r\n# Get Proxy from Internet Explorer\r\noWebClient.proxy=&#x5B;System.Net.WebProxy]::GetDefaultProxy()\r\n$oRSSFeed=&#x5B;&#x5B;xml]]$oWebClient.DownloadString(\"http:\/\/michlstechblog.info\/blog\/feed\/\")\r\n$oRSSFeed.rss.channel.item|foreach{write-host $_.title}\r\n<\/pre>\n<p>Copy a XML Node from one document to another<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\n# Get XML Document Object from String\r\n&#x5B;&#x5B;xml]]$oConfig2Merge=&#039;&lt;config description=&quot;Users&quot;&gt;\r\n  &lt;customers&gt;\r\n    &lt;user surname=&quot;Campbell&quot; firstname=&quot;Wayne&quot;&gt;Wayne&lt;\/user&gt;\r\n    &lt;user surname=&quot;Garth&quot; firstname=&quot;Algar&quot;&gt;Garth&lt;\/user&gt;\r\n    &lt;user surname=&quot;Cheers&quot; firstname=&quot;Phil&quot;&gt;Phil&lt;\/user&gt;\r\n  &lt;\/customers&gt;\r\n&lt;\/config&gt;&#039;\r\n# Migrate the customers node to our config file defined at the top of the post\r\n# Get the target config Node\r\n$oXMLConfigTarget=$oXMLDocument.selectSingleNode(&quot;config&quot;)\r\n# Get the Node which is to migrate\r\n$oXMLCustomers=$oConfig2Merge.config.customers\r\n# or\r\n$oXMLCustomers=$oConfig2Merge.selectSingleNode(&quot;config\/customers&quot;)\r\n# Import the Node with the ImportNode Function from System.XML.XMLDocument\r\n$oXMLConfigTarget.appendChild($oXMLDocument.ImportNode($oXMLCustomers,$true))\r\n$oXMLDocument.Save(&quot;c:\\temp\\config.xml&quot;)\r\n<\/pre>\n<p>To be continued&#8230;<\/p>\n<p>Michael<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi everybody, here are some fundamentals to handle XML Files with powershell. The following commands uses these XML File &lt;config description=&quot;Config file for testing&quot;&gt; &lt;system description=&quot;Document Management&quot;&gt; &lt;document authorFirstName=&quot;Kirk&quot; authorSurname=&quot;Author1&quot; date=&quot;20130503 01:15:43&quot; description=&quot;Document Hammet&quot; index=&quot;3&quot;&gt;C:\\temp\\doc0001.txt&lt;\/document&gt; &lt;document authorFirstName=&quot;Lars&quot; authorSurname=&quot;Author5&quot; date=&quot;20130503 10:05:42&quot; description=&quot;Document Ulrich&quot; index=&quot;4&quot;&gt;C:\\temp\\doc0002.txt&lt;\/document&gt; &lt;document authorFirstName=&quot;Cliff&quot; authorSurname=&quot;Author5&quot; date=&quot;20130612 11:54:33&quot; description=&quot;Document Burton&quot;\u00a0 index=&quot;6&quot;&gt;C:\\temp\\doc0051.txt&lt;\/document&gt; &lt;document authorFirstName=&quot;Jason&quot; authorSurname=&quot;Author2&quot; date=&quot;20130806 &hellip; <a href=\"https:\/\/michlstechblog.info\/blog\/powershell-some-basic-xml-handling-with-powershell-and-net\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Powershell: Some basic XML handling with Powershell and .NET<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,152],"tags":[76,153,143,156,160,158,154,155,133,157,159,147,20,861],"class_list":["post-603","post","type-post","status-publish","format-standard","hentry","category-windowsscripts","category-xml","tag-net","tag-copy","tag-create","tag-document","tag-element","tag-load","tag-node","tag-other","tag-powershell","tag-save","tag-setattribute","tag-to","tag-windows-2","tag-xml"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/posts\/603","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/comments?post=603"}],"version-history":[{"count":19,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/posts\/603\/revisions"}],"predecessor-version":[{"id":7552,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/posts\/603\/revisions\/7552"}],"wp:attachment":[{"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/media?parent=603"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/categories?post=603"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/tags?post=603"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}