0

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

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Hi LeeHarvey1, thanks a lot for your help and explanation. It works fine, and solves my issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.