Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

284
Visualizações
Is there a reliable way to get the selected element's names after applying an XPath expression with select-xml

I am struggling to find a reliable way to determine the names of elements that were selected by an XPath expression using the PowerShell cmdlet select-xml:

$xml = select-xml -content @'
<root>
  <A name='foo' localname='FOO'/>
  <B name='bar'                />
  <C id  ='baz' localname='BAZ'/>
  <D                           />
</root>
'@  -XPath '/root/*'

[System.Xml.XmlElement] $elem = $null
foreach ($elem in $xml.Node) {
   "name = $($elem.name), localname = $($elem.localname)"
}

This code prints

name = foo, localname = FOO
name = bar, localname = B
name = C, localname = BAZ
name = D, localname = D

Apparently, the XML attributes name and localname interfere with the .NET class attributes with the same names. So, is there a construct that returns A, B, C and D for the above example.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

XMLElement class allows matching attribute names and XMLElement class property names. In that case, the attribute names take precedence during member access (object.property). The properties created when XMLElement object is instanced can be retrieved with Get_ methods (Get_Name() and Get_LocalName() in this scenario). The attributes names can be retrieved with the GetAttribute method to ensure a consistent experience.

$xml = select-xml -content @'
<root>
  <A name='foo' localname='FOO'/>
  <B name='bar'                />
  <C id  ='baz' localname='BAZ'/>
  <D                           />
</root>
'@  -XPath '/root/*'
$xml.Node |% {
    # XMLElement class property Name and LocalName values
    "Name = {0}, LocalName = {1}" -f $_.Get_Name(),$_.Get_LocalName() 
    # Value of attributes name and localname
    "name = {0}, localname = {1}" -f $_.GetAttribute('name'),$_.GetAttribute('localname')
}
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda