Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get tag name of XML element that has a "name" property

Tags:

powershell

xml

$x = ([xml]"<sample name='notsample'/>").sample

Given the above line of code, as is, I want to find the name of the tag described by the XmlElement in $x. Typically, you would just use $x.Name, but the name attribute masks it. Instead of returning sample, $x.name returns notsample.

The only workaround I've found is:

[Xml.XmlElement].GetProperty("Name").GetValue($x)

... but this is hacky. How can I do this correctly?

like image 402
David Pfeffer Avatar asked Sep 08 '14 18:09

David Pfeffer


1 Answers

You can get it by invoking the property getter method directly:

$x.get_Name()

This works in many other similar cases. For example, if a type implements IDictonary and has other properties, you may need to use this to access those properties. By default, PowerShell does a lookup into the dictionary rather than getting/setting the accessed property.

You can get PowerShell to show hidden members like these using the -Force option on Get-Member:

$x | gm -Force
like image 183
Mike Zboray Avatar answered Nov 15 '22 06:11

Mike Zboray