Last active
October 23, 2019 13:36
-
-
Save wightsci/dd349c42f43ccef488de1edf4008ced0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Function Format-XMLText { | |
Param( | |
[Parameter(ValueFromPipeline=$true,Mandatory=$true)] | |
[xml[]] | |
$xmlText | |
) | |
Process { | |
# Use a StringWriter, an XMLWriter and an XMLWriterSettings to format XML | |
$stringWriter = New-Object System.IO.StringWriter | |
$stringWriterSettings = New-Object System.Xml.XmlWriterSettings | |
# Turn on indentation | |
$stringWriterSettings.Indent = $true | |
# Turn off XML declaration | |
$stringWriterSettings.OmitXmlDeclaration = $true | |
# Create the XMLWriter from the StringWriter | |
$xmlWriter = [System.Xml.XmlWriter]::Create($stringWriter,$stringWriterSettings) | |
# Write the XML using the XMLWriter | |
$xmlText.WriteContentTo($xmlWriter) | |
# Don't forget to flush! | |
$xmlWriter.Flush() | |
$stringWriter.Flush() | |
# Output the text | |
$stringWriter.ToString() | |
# This works in a remote session, when [Console]::Out doesn't | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment