Reputation: 39862
$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?
Upvotes: 7
Views: 5409
Reputation: 40828
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
Upvotes: 11
Reputation: 54911
What you are after is the LocalName
property of the XmlElement
class. Try:
PS> $x.LocalName
sample
Gets the local name of the current node.
Property Value
Type: System.String
The name of the current node with the prefix removed. For example, LocalName is book for the element
<bk:book>
.
Source: MSDN: XmlElement.LocalName
Upvotes: 4