Types/OpenPackage.Part/WriteXml.ps1

<#
.SYNOPSIS
    Writes Part Content XML
.DESCRIPTION
    Writes an OpenPackage Part's Content as XML
#>

[Reflection.AssemblyMetadata(
    'FilePattern', '\.(?>svg|nuspec|ps1xml|xml|xhtml|proj)$'
)]
[Reflection.AssemblyMetadata(
    'ContentTypePattern', '[/\+](?>xml|xsd|xslt?)$'
)]
param(
# The object to write.
[Alias('Input','Content','Text')]
[PSObject]
$InputObject,

<#

Any options used to write the object

Supported Options:

|Option|Description|
|-|-|
|Encoding|The text encoding|
|Stream|Optional destination stream|
#>


[Collections.IDictionary]
$Option = [Ordered]@{}
)

if (-not $this.WriteText) { throw 'No `.WriteText()`'; return }

# If the content is aleady xml
if ($InputObject -is [xml]) {
    # write the content
    $this.WriteText($InputObject.OuterXml, $option)
    return
}

# Otherwise, make the content a string array and join it with nothing
$stringified = $InputObject -as 'string[]' -join ''
# this will coalesce output into a form that might be xml.
# By casting, we will see any error in conversion.
$contentAsXml = [xml]$stringified
# If conversion to xml worked,
if ($contentAsXml) {    
    $this.WriteText($stringified, $option) # write our string
    return
}