{"id":8044,"date":"2021-11-22T23:21:01","date_gmt":"2021-11-22T22:21:01","guid":{"rendered":"https:\/\/michlstechblog.info\/blog\/?p=8044"},"modified":"2021-11-23T11:03:32","modified_gmt":"2021-11-23T10:03:32","slug":"powershell-write-xml-files-with-unix-line-endings","status":"publish","type":"post","link":"https:\/\/michlstechblog.info\/blog\/powershell-write-xml-files-with-unix-line-endings\/","title":{"rendered":"Powershell: Write XML files with UNIX line endings"},"content":{"rendered":"<div class=\"twoclick_social_bookmarks_post_8044 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_8044')){$('.twoclick_social_bookmarks_post_8044').socialSharePrivacy({\"services\":{\"flattr\":{\"uid\":\"Michl\",\"status\":\"on\",\"the_title\":\"Powershell%3A%20Write%20XML%20files%20with%20UNIX%20line%20endings\",\"the_excerpt\":\"Hi%2C%0D%0A%0D%0Aif%20you%20want%20to%20create%20XML%20files%20with%20powershell%20which%20can%20be%20used%20on%20UNIX%2FLinux%20systems%20you%20need%20to%20write%20an%20own%20save%20function.%20Ths%20save%28%29%20methode%20of%20the%20System.XML.XMLDocument%20class%20can%20only%20write%20Windows%20style%20XML%20files%20with%20Carriage%20return%2FNew%20line%20endings.%0D%0A%20%28more%26hellip%3B%29\",\"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-write-xml-files-with-unix-line-endings\\\/\",\"post_id\":8044,\"post_title_referrer_track\":\"Powershell%3A+Write+XML+files+with+UNIX+line+endings\",\"display_infobox\":\"on\"});}});\n\/* ]]> *\/<\/script><\/div><p>Hi,<\/p>\n<p>if you want to create XML files with powershell which can be used on UNIX\/Linux systems you need to write an own save function. Ths save() methode of the System.XML.XMLDocument class can only write Windows style XML files with Carriage return\/New line endings.<br \/>\n<!--more--><br \/>\nThis is a function which write Linux\/UNIX style UTF8 files<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\nfunction Write-UnixXml {\r\n    &#x5B;CmdletBinding()]\r\n    param(\r\n        &#x5B;Parameter(Mandatory=$true, Position=0)]&#x5B;xml]$xmlStructure,\r\n        &#x5B;Parameter(Mandatory=$true, Position=1)]&#x5B;string]$FullPath\r\n    )\r\n    $oXMLWriterSettings = new-object System.Xml.XmlWriterSettings\r\n    $oXMLWriterSettings.NewLineChars = &quot;`n&quot;                                      \r\n    $oXMLWriterSettings.Indent       = $true                                     \r\n    $oXMLWriterSettings.IndentChars  = &quot;`t&quot;\r\n    $oXMLWriterSettings.NewLineHandling = 0\r\n    $oXMLWriterSettings.Encoding     = new-object System.Text.UTF8Encoding  \r\n    try\r\n    {\r\n        $oXMLWriter = &#x5B;System.Xml.XmlTextWriter]::Create($FullPath, $oXMLWriterSettings)\r\n        $xmlStructure.WriteTo($oXMLWriter)\r\n        $oXMLWriter.Flush()\r\n        $oXMLWriter.Close()\r\n    }\r\n\tcatch\r\n\t{\r\n\t\twrite-host (&#x5B;string]::Format(&quot; Error occured while writing XML file {0}. Errormessage: {1})&quot;,$FullPath,$_.Exception.Message))\r\n\t}\r\n}\r\n<\/pre>\n<p>Usage<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\nPS D:\\&gt; &#x5B;&#x5B;xml]] $TestXML='&lt;config&gt;MyConfig &lt;Value number=&quot;1&quot;&gt;&lt;\/Value&gt;&lt;\/config&gt;'\r\nPS D:\\&gt; Write-UnixXml -xmlStructure $TestXML -FullPath D:\\temp\\text.xml\r\n<\/pre>\n<p>Michael<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi, if you want to create XML files with powershell which can be used on UNIX\/Linux systems you need to write an own save function. Ths save() methode of the System.XML.XMLDocument class can only write Windows style XML files with Carriage return\/New line endings.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[344,4,152],"tags":[],"class_list":["post-8044","post","type-post","status-publish","format-standard","hentry","category-powershell-scripting","category-windowsscripts","category-xml"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/posts\/8044","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=8044"}],"version-history":[{"count":15,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/posts\/8044\/revisions"}],"predecessor-version":[{"id":8058,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/posts\/8044\/revisions\/8058"}],"wp:attachment":[{"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/media?parent=8044"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/categories?post=8044"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/tags?post=8044"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}