<<< Back to Search

XML

Get Element/Attribute using XPath Filtering

Get element and/or attributes from XML file using XPath Filtering

Snippets based on this sample xml:


# XML ::: Get Element/Attribute using XPath
# 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 XPath
# www.PowerShellExamples.com

# GETTING ELEMENTS

# Get all Elements of same type (Use method SelectNodes)
$xmlElement = $xmlFile.SelectNodes("/root/Person/FirstName").InnerText

# Get first Element (Use method SelectSingleNode)
$xmlElement = $xmlFile.SelectSingleNode("/root/Person/FirstName").InnerText

# Get Element filtering string
$xmlElement = $xmlFile.SelectNodes('/root/Person[FirstName="Carla"]')

# Get Element filtering by Attribute
$xmlElement = $xmlFile.SelectNodes('/root/Person[@Gender="Male"]')



# GETTING ATTRIBUTES
# *Attributes are case sensitive

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

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

Practical Sample




# XML > Get Element/Attribute using XPath
# www.PowerShellExamples.com

# 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>
"@

# Get Element



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

# Get all Elements of same type (Use method SelectNodes)
$xmlElement = $xmlFile.SelectNodes("/root/Person/FirstName").InnerText

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


# Get first Element (Use method SelectSingleNode)
$xmlElement = $xmlFile.SelectSingleNode("/root/Person/FirstName").InnerText

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


# Get Element filtering string
$xmlElement = $xmlFile.SelectNodes('/root/Person[FirstName="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.SelectNodes('/root/Person[@Gender="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 (All Genders) (Use method SelectNodes)
# *Attributes are case sensitive
$xmlAttribute = $xmlFile.SelectNodes("/root/Person").GetAttribute("Gender") 

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


# Get Attribute from First element (Use method SelectSingleNode)
$xmlAttribute = $xmlFile.SelectSingleNode("/root/Person").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