<<< Back to Search

XML

Get Element/Attribute using Object Notation

Get element and/or attributes from XML file using Object Notation

Snippets are based on this sample xml:


# XML ::: Get Element/Attribute using Object Notation
# www.PowerShellExamples.com

# Snippets based on this sample xml
[xml]$xmlFile = @"
<root>
    <Person Gender="Female" id="1">
        <FirstName>Carla</FirstName>
        <LastName>Trevor</LastName>
    </Person>
    <Person Gender="Male">
        <FirstName>Charlie</FirstName>
        <LastName>Garcia</LastName>
    </Person>
</root>
"@

Core Snippet


# XML ::: Get Element/Attribute using Object Notation
# www.PowerShellExamples.com


# GETTING ELEMENTS

# Get all Elements of same type (All FirstNames)
$xmlElement = $xmlFile.root.Person.FirstName

# Get first Element (base 0 array)
$xmlElement = $xmlFile.root.Person.FirstName[0]

# Get Element filtering string
$xmlElement = $xmlFile.root.Person | Where-Object { $_.FirstName -eq "Carla"}

# Get Element filtering by Attribute
$xmlAttribute = $xmlFile.root.Person | Where-Object { $_.GetAttribute("Gender") -eq "male"}


# GETTING ATTRIBUTES
# *Attributes are case sensitive

# Get Attribute from all Elements (All Genders)
$xmlAttribute = $xmlFile.root.Person.GetAttribute("Gender")  

# Get Attribute from First element (base 0 array)
$xmlAttribute = $xmlFile.root.Person[0].GetAttribute("Gender")


Practical Sample

# create an xml file with a "here string"
[xml]$xmlFile = @"
<root>
    <Person Gender="Female" id="1">
        <FirstName>Carla</FirstName>
        <LastName>Trevor</LastName>
    </Person>
    <Person Gender="Male">
        <FirstName>Charlie</FirstName>
        <LastName>Garcia</LastName>
    </Person>
</root>
"@
# XML ::: Get Element/Attribute using Object Notation
# www.PowerShellExamples.com

Write-Host "`r`nGETTING ELEMENTS" -ForegroundColor Cyan
# Get all Elements of same type
$xmlElement = $xmlFile.root.Person.FirstName

Write-Host "`r`nAll elements of same type - All FirstNames:"
$xmlElement | Out-String | Write-Host

# Get first Element (base 0 array)
$xmlElement = $xmlFile.root.Person.FirstName[0]

Write-Host "`r`nFirst element- FirstName from first Person:"
$xmlElement | Out-String | Write-Host


# Get Element filtering string
$xmlElement = $xmlFile.root.Person | Where-Object { $_.FirstName -eq "Carla"}

Write-Host "`r`nElements filtered by string - Filter Person with FirstName `"Carla`":"
$xmlElement | Out-String | Write-Host


# Get Element filtering by Attribute
$xmlElement = $xmlFile.root.Person | Where-Object { $_.GetAttribute("Gender") -eq "male"}

Write-Host "`r`nElements filtered by attribute - Filter `"Gender: Male`":"
$xmlElement | Out-String | Write-Host


Write-Host "`r`nGETTING ATTRIBUTES" -ForegroundColor Cyan

# Get Attribute from all Elements
# *Attributes are case sensitive
$xmlAttribute = $xmlFile.root.Person.GetAttribute("Gender")  

Write-Host "`r`nAttribute Gender from All elements:"
$xmlAttribute | Out-String | Write-Host


# Get Attribute from First element (base 0 array)
$xmlAttribute = $xmlFile.root.Person[0].GetAttribute("Gender")
Write-Host "`r`nAttribute Gender from First element:"
$xmlAttribute | Out-String | Write-Host


Result

PS C:\>

GETTING ELEMENTS

All elements of same type - All FirstNames:
Carla
Charlie


First element- FirstName from first Person:
Carla


Elements filtered by string - Filter Person with FirstName "Carla":

Gender id FirstName LastName
------ -- --------- --------
Female 1  Carla     Trevor




Elements filtered by attribute - Filter "Gender: Male":

Gender FirstName LastName
------ --------- --------
Male   Charlie   Garcia




GETTING ATTRIBUTES

Attribute Gender from All elements:
Female
Male


Attribute Gender from First element:
Female