Reputation: 361
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
Reputation: 174720
Use the HasAttribute()
method to ascertain whether the attribute already exists:
if(-not $xml.METATRANSCRIPT.HasAttribute('ArchiveHandle')){
# ...
}
Upvotes: 4