Learn about Windows PowerShell
Summary: Use the [xml] type accelerator to greatly simplify reading XML documents.
How can I easily read an XML file?
Use the [XML] type accelerator to convert results from Get-Content into an XML document, and then use dotted notation to access the nodes:
[xml]$books = Get-Content C:\fso\Books.XML
$books.catalog.book.title
Hi Ed,
thanks for sharing....here a proxy function:
Function Get-Content {
<#
------------EXAMPLE---------------8<-----------------------------------------
PS II> $file = "$PSHOME\types.ps1xml"
PS II> $u = cat $file -As | Select-Xml -XP "//ScriptProperty" | Select -Expand Node
PS II> $u
PS II> $Xml = Get-Content $file -AsXml
PS II> $Xml.Types.Type[1..10]
---------------------------8<---------------------AUTHOR: Walid Toumi-------
.ForwardHelpTargetName Get-Content
.ForwardHelpCategory Cmdlet
#>
[CmdletBinding(DefaultParameterSetName='Path', SupportsTransactions=$true)]
param(
[Parameter(ValueFromPipelineByPropertyName=$true)]
[System.Int64]
${ReadCount},
${TotalCount},
[Parameter(ParameterSetName='Path', Mandatory=$true, Position=0, ValueFromPipelineByPropertyName=$true)]
[System.String[]]
${Path},
[Parameter(ParameterSetName='LiteralPath', Mandatory=$true, Position=0, ValueFromPipelineByPropertyName=$true)]
[Alias('PSPath')]
${LiteralPath},
[System.String]
${Filter},
${Include},
${Exclude},
[Switch]
${Force},
${AsXml},
[System.Management.Automation.PSCredential]
${Credential})
begin
{
try {
$outBuffer = $null
if ($PSBoundParameters.TryGetValue('OutBuffer', [ref]$outBuffer))
$PSBoundParameters['OutBuffer'] = 1
}
$wrappedCmd = $ExecutionContext.InvokeCommand.GetCommand('Microsoft.PowerShell.Management\Get-Content', [System.Management.Automation.CommandTypes]::Cmdlet)
$cmd = ''
if($AsXml) {
[void]$PSBoundParameters.Remove('AsXml')
$cmd += ' | ForEach-Object {$fx=@()} {$fx+=$_} {$fx -as [Xml]}'
$ScriptCmd = [ScriptBlock]::Create(
{ & $wrappedCmd @PSBoundParameters }.ToString() + $Cmd
)
$steppablePipeline = $scriptCmd.GetSteppablePipeline($myInvocation.CommandOrigin)
$steppablePipeline.Begin($PSCmdlet)
} catch {
throw
process
$steppablePipeline.Process($_)
end
$steppablePipeline.End()
#########
PS II> $xml= cat c:\xmlfile.xml -asxml ; $xml
@Walid Toumi this is great. Thank you for sharing. Would you consider uploading this to the scripting guys script repository? It would make it eaiser to copy.
Very nice. I have used this feature quite often already to import collections / packages / etc. into SCCM, based on an XML input file!
I'm using PowerShell 3 and tried your example without the [xml] accelerator and everything seemed to work fine. What does the accelerator do and is really needed?
@Craig the [xml] type accelerator converts the text into an XMLDocument type of object. This permits navigation of the pieces of the document using a dotted notation. Without it, you only have text and not XML. See my article I wrote here: blogs.technet.com/.../use-powershell-to-simplify-access-to-xml-data.aspx
@all -
Note that this will not work if the content is wrapped in a namespace. To resolve namespaces you will need to either use the namespace manager or use the Select-XML CmdLet with its namespace support.
I think hear Ed madly typing a blog article on this.
Excelente! Simples e direto!!! (Y)