private/Format-XML.ps1

<#
    .SYNOPSIS
    Formats XML content for improved readability.
 
    .DESCRIPTION
    The Format-XML function takes XML content as input and formats it to improve readability. It creates an XmlDocument object, loads the provided XML content into it, and then uses an XmlTextWriter to write the formatted XML to a string. This makes the XML content easier to read and understand.
 
    .PARAMETER XMLContent
    Specifies the XML content to be formatted. This parameter is mandatory and accepts XML content as a string.
 
    .EXAMPLE
    PS C:\> $XMLContent = Format-XML "<root><item>Value 1</item><item>Value 2</item></root>"
    PS C:\> Format-XML -XMLContent $XMLContent
 
    This example formats the provided XML content and displays the formatted XML as output.
 
    .NOTES
        Function : Format-XML
        Author : John Billekens
        Copyright: Copyright (c) AppVentiX
        Version : 1.0
#>

function Format-XML {
    [CmdletBinding()]
    Param (
        [Parameter(ValueFromPipeline = $true, Mandatory = $true, Position = 0)]
        [Alias("Data", "Content")]
        [string]$XMLContent
    )
    Begin {
    }
    Process {
        $xmlObject = New-Object -TypeName System.Xml.XmlDocument
        $xmlObject.LoadXml($XMLContent)
        $stringWriter = New-Object System.IO.StringWriter
        $xmlTextWriter = New-Object System.Xml.XmlTextwriter($stringWriter)
        $xmlTextWriter.Formatting = [System.XML.Formatting]::Indented
        $xmlObject.WriteContentTo($xmlTextWriter)
        Write-Output -InputObject $($stringWriter.ToString())
        $xmlObject = $null
        $stringWriter.Dispose()
        $xmlTextWriter.Dispose()
    }
    End {
        $stringWriter = $null
        $xmlTextWriter = $null
    }
}