Paulo
Paulo

Reputation: 361

Powershell Check if attributes already exists on xml node

I try to check if an attribute already exists in certain node, don`t gives-me any error, but always replace the attribute for the new one created, even if exists.

My code:

    [xml]$xml = Get-Content $_.FullName 
    $xml= $xml.METATRANSCRIPT.OuterXml

   if(-not $xml.METATRANSCRIPT.HasAttribute('ArchiveHandle')
    {
   $xmlAtt = $xml.CreateAttribute("ArchiveHandle");
   $dt=$xml.METATRANSCRIPT.Attributes.GetNamedItem('Date');
   $xmlAtt=$xml.METATRANSCRIPT.Attributes.InsertBefore($xmlAtt, $dt)
    }
    else {...}

If the attribute already exists do : ...something else.., if don`t exists create it.

Thanks for any help.

Upvotes: 0

Views: 3514

Answers (1)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174720

Use the HasAttribute() method to ascertain whether the attribute already exists:

if(-not $xml.METATRANSCRIPT.HasAttribute('ArchiveHandle')){
    # ...
}

Upvotes: 4

Related Questions