xmlpowershelltivoli-work-scheduler

How to read .xml into memory and write out the same result


I am trying to read a .xml file, change some values (not yet), and write it back out. Without making any changes, I expect to get the same thing comeing out as went in. It does not.

PS H:\src\tws> type .\test000.xml
<?xml version="1.0"?>
<eventRuleSet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xmlns="http://www.ibm.com/xmlns/prod/tws/1.0/event-management/rules"
              xsi:schemaLocation="http://www.ibm.com/xmlns/prod/tws/1.0/event-management/rules http://www.ibm.com/xmlns/prod/tws/1.0/event-management/rules/EventRules.xsd">
    <eventRule name="PW-TEST001" ruleType="filter" isDraft="no">
        <description>Paul's test001</description>
    </eventRule>
</eventRuleSet>

Here is the simple code I am using to read it in and write it out.

PS H:\src\tws> Get-Content .\con000.ps1
$x = [xml](Get-Content -Path .\test000.xml)
$x | Export-Clixml -Path .\con000.xml -Encoding utf8

The output has and sections. Why is that? I would like to get out what went in. I do not care about newlines or the use of HTML entities. I just want the content to be the seme. Yes, the plan is to read a template, change some values, and output a new .xml file. This will be input to the IBM/HCS Workload Scheduler.

PS H:\src\tws> type .\con000.xml
<Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04">
  <XD>&lt;?xml version="1.0"?&gt;&lt;eventRuleSet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.ibm.com/xmlns/prod/tws/1.0/event-management/rules" x
si:schemaLocation="http://www.ibm.com/xmlns/prod/tws/1.0/event-management/rules http://www.ibm.com/xmlns/prod/tws/1.0/event-management/rules/EventRules.xsd"&gt;&lt;eventRule
name="PW-TEST001" ruleType="filter" isDraft="no"&gt;&lt;description&gt;Paul's test001&lt;/description&gt;&lt;/eventRule&gt;&lt;/eventRuleSet&gt;</XD>
</Objs>

Solution

  • In order to save a text representation of an [xml] instance to a file, you have two basic choices:


    Here's a simple example with .OuterXml:

    # Read the input file into an XML document (in-memory DOM).
    $x = [xml] (Get-Content -Raw ./test000.xml)
    
    # Make updates to the in-memory document
    $x.eventRuleSet.eventRule.description = 'new description'
    
    # Save the modified document as text to an output file,
    # using the un-prettied textual representation provided by the .OuterXml
    # property.
    # If *BOM-less* UTF-8 encoding is what you want, simply use
    #   $x.Save("$PWD/con000.xml")
    # In PowerShell *Core*, you'd get BOM-less UTF-8 even with the command below.
    $x.OuterXml | Set-Content -Encoding utf8 ./con000.xml
    

    A note re the use of a BOM (a.k.a. Unicode signature) with UTF-8 and other Unicode encodings:

    For best overall compatibility, BOMs in UTF-8 files should be avoided: Unix platforms and Unix-heritage utilities also used on Windows Platforms generally don't know how to handle them.

    Similarly, -Encoding UTF7 should be avoided, because it is not a standard Unicode encoding (and is written without a BOM in both PowerShell editions).

    In both PowerShell editions, all other Unicode encodings available with -Encoding do create an (encoding-appropriate) BOM: Unicode (UTF-16LE), bigendianunicode (UTF-16BE), and utf32 (UTF-32).