PSXml.psm1

#Region './Public/Format-Xml.ps1' 0

<#
    .SYNOPSIS
        Formats an XML document and returns a string.

    .DESCRIPTION
        Formats an XML document and returns a string.

    .PARAMETER XmlDocument
        The XML document to format.

    .PARAMETER Indented
        Specifies if the XML document should be formatted with indentation.

    .EXAMPLE
        Format-Xml -XmlDocument '<?xml version="1.0"?><a><b /></a>' -Indented

    .EXAMPLE
        $xmlResult | Format-Xml -Indented

#>

function Format-Xml
{
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseProcessBlockForPipelineCommand', '')]
    [OutputType([System.String])]
    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
        [System.Xml.XmlDocument]
        $XmlDocument,

        [Parameter()]
        [Switch]
        $Indented
    )

    $xmlSettings = New-Object -TypeName 'System.Xml.XmlWriterSettings'

    if ($Indented.IsPresent)
    {
        $xmlSettings.Indent = $true
    }
    else
    {
        $xmlSettings.Indent = $false
    }

    $xmlOutput = New-Object -TypeName 'System.Text.StringBuilder'

    $xmlWriter = [System.Xml.XmlWriter]::Create($xmlOutput, $xmlSettings)

    $XmlDocument.Save($xmlWriter)

    $XmlWriter.Flush()
    $xmlWriter.Close()

    return $xmlOutput.ToString()
}
#EndRegion './Public/Format-Xml.ps1' 60
#Region './Public/Get-XmlAttribute.ps1' 0

<#
    .SYNOPSIS
        Returns a hashtable containing all the attributes in the given search query.

    .DESCRIPTION
        This command returns a hashtable containing all the attributes in the
        path provided in the parameter XPath.


    .PARAMETER XmlDocument
        Specifies an XML document to perform the search query on.

    .PARAMETER XPath
        Specifies an XPath search query.

    .EXAMPLE
        $xmlResult | Get-XmlAttribute -XPath '/report/counter[@type="LINE"]'

#>

function Get-XmlAttribute
{
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseProcessBlockForPipelineCommand', '')]
    [CmdletBinding()]
    [OutputType([System.Collections.Hashtable])]
    param
    (
        [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
        [System.Xml.XmlDocument]
        $XmlDocument,

        [Parameter(Mandatory = $true)]
        [System.String]
        $XPath
    )

    $attributeValues = @{}

    $filteredDocument = $XmlDocument | Select-Xml -XPath $XPath

    ($filteredDocument.Node | Select-Xml -XPath '@*').Node | ForEach-Object -Process {
        $attributeValues[$_.Name] = $_.Value
    }

    return $attributeValues
}
#EndRegion './Public/Get-XmlAttribute.ps1' 47
#Region './Public/Out-Xml.ps1' 0

<#
    .SYNOPSIS
        Outputs an XML document to a file.

    .DESCRIPTION
        Outputs an XML document to the file specified in the parameter Path.

    .PARAMETER XmlDocument
        The XML document to format.

    .PARAMETER Path
        The path to the file name to write to.

    .PARAMETER Encoding
        Specifies the encoding for the file.

    .EXAMPLE
        Out-Xml -XmlDocument '<?xml version="1.0"?><a><b /></a>' -Indented
#>

function Out-Xml
{
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseShouldProcessForStateChangingFunctions', '')]
    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory = $true)]
        [System.Xml.XmlDocument]
        $XmlDocument,

        [Parameter(Mandatory = $true)]
        [System.String]
        $Path,

        [Parameter()]
        [ValidateSet('UTF8')]
        [System.String]
        $Encoding = 'UTF8'
    )

    $xmlSettings = New-Object -TypeName 'System.Xml.XmlWriterSettings'

    $xmlSettings.Encoding = [System.Text.Encoding]::$Encoding

    $xmlWriter = [System.Xml.XmlWriter]::Create($Path, $xmlSettings)

    $XmlDocument.Save($xmlWriter)

    $XmlWriter.Flush()
    $xmlWriter.Close()
}
#EndRegion './Public/Out-Xml.ps1' 52