Paulo
Paulo

Reputation: 361

Powershell Append () attribute on xml defined position

I need to add an attribute on root element, but in certain position:

<METATRANSCRIPT xmlns="http://www.mpi.nl/IMDI/Schema/IMDI" 
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
     Date="2016-01-29" FormatId="IMDI 3.03" Originator="" Type="SESSION" 
     Version="0" 
     xsi:schemaLocation="http://www.mpi.nl/IMDI/Schema/IMDI ./IMDI_3.0.xsd" 
     ArchiveHandle="">

The attribute ArchiveHandle="" needs to stay between xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" and Date="2016-01-29".

How can I solve this, and put the attribute in the right place?

This is my code:

   Get-ChildItem -Path 'Path\To\XML\Files' -Recurse -Include "*.imdi" -File | ForEach-Object
  {

  [xml]$xml = Get-Content $_.FullName; 
  $xml= $xml.METATRANSCRIPT.OuterXml;
  $xmlAtt = $xml.CreateAttribute("ArchiveHandle")
  $xsi= $xml.DocumentElement.xsi
  $xmlAttRef = $xml.DocumentElement.Attributes.Append($xmlAtt)
  $xml.Save($_.FullName)
  }

Thanks for any help

Upvotes: 0

Views: 124

Answers (1)

leeharvey1
leeharvey1

Reputation: 1446

As you've discovered, the Attributes.Append method will always append new attributes at the end. Thus, what you really want to use is either Attributes.InsertBefore method or Attributes.InsertAfter method.

For example:

Get-ChildItem -Path 'Path\To\XML\Files' -Recurse -Include '*.imdi' -File | ForEach-Object {
    [xml]$xml = Get-Content -Path $_.FullName
    if ($xml.METATRANSCRIPT.HasAttribute('ArchiveHandle'))
    {
        $xml.METATRANSCRIPT.RemoveAttribute('ArchiveHandle')
    }

    $ah = $xml.CreateAttribute('ArchiveHandle')
    $dt = $xml.METATRANSCRIPT.Attributes.GetNamedItem('Date')
    $ah = $xml.METATRANSCRIPT.Attributes.InsertBefore($ah, $dt)
    $xml.Save($_.FullName)
}

In my snippet above, I am explicitly removing any pre-existing ArchiveHandle attribute. Then, after creating a new ArchiveHandle attribute, I get the XmlAttribute of the item I want to insert my new attribute before (eg, Date), then call the Attributes.InsertBefore method accordingly. I could have easily chosen xmlns:xsi then called the Attributes.InsertAfter method. Finally, I save the resulting XML (for each file found by Get-ChildItem).

Hope this helps.

Upvotes: 1

Related Questions