Problem adding element to XML file

Welcome Forums General PowerShell Q&A Problem adding element to XML file

This topic contains 5 replies, has 3 voices, and was last updated by

 
Participant
3 months ago.

  • Author
    Posts
  • #161151

    Participant
    Topics: 2
    Replies: 2
    Points: 32
    Rank: Member

    Hi all,

    I am new to Powershell so please be gentle. I am trying to add a new element to several sections of an XML document.

    $xml = [xml](Get-Content "test.xml")
    
    $root = $xml.DocumentElement
    $child = $xml.CreateElement("ENV")
    $child.SetAttribute("Id","NewID")
    $child.InnerXML = "New Text"
    $nodes = $xml.SelectNodes("//ENV[@Id='E3']")
    foreach ($node in $nodes) {
        $parent = $node.ParentNode
        $parent.InsertAfter($child,$parent.FirstChild)
        $parent.outerXML
    }
    $xml.save("test2.xml")
    

    test.xml

    When I run this the visual output looks correct, with 4 new elements shown, but the saved XML file only has 1 new element in it (the last occurence). I am totally confused and hope that someone can tell me what I have done wrong!

    TIA.

  • #161493

    Participant
    Topics: 2
    Replies: 2
    Points: 32
    Rank: Member

    BUMP. Is anyone able to tell me what I am doing wrong here? Thanks.

  • #161531

    Senior Moderator
    Topics: 8
    Replies: 1041
    Points: 3,439
    Helping Hand
    Rank: Community Hero

    on top of my head : Try putting the save inside the loop.

  • #161645

    Participant
    Topics: 2
    Replies: 999
    Points: 1,946
    Helping Hand
    Rank: Community Hero

    This is not unique to PowerShell. This would have happened to you doing it this way in any language.

    It's because you have your needed elements outside of the loop , and thus have one. To repeat it per node, you need to tell the language to do so.

    $xml = [xml](Get-Content '.\test.xml')
    
    $root = $xml.DocumentElement
    $nodes = $xml.SelectNodes("//ENV[@Id='E3']")
    
    foreach ($node in $nodes) 
    {
        $child = $xml.CreateElement("ENV")
        $child.SetAttribute("Id","NewID")
        $child.InnerXML = "New Text"
    
        $parent = $node.ParentNode
        $parent.InsertAfter($child,$parent.FirstChild)
    }
    
    
    # Results
    
    < #
    Id    #text   
    --    -----   
    NewID New Text
    NewID New Text
    NewID New Text
    NewID New Text
    #>
    
    $xml.save('.\test2.xml')
    Get-Content -Path '.\test2.xml'
    
    # Results
    < #
    
      
        E1
        New Text
        E2
        E3
        D1
        
          S1
          M1
          M2
        
      
      
        E1
        New Text
        E2
        E3
        D1
        W1
      
      
        E1
        New Text
        E2
        E3
        D1
      
      
        E1
        New Text
        E2
        E3
        D1
      
    
    #>
  • #161678

    Participant
    Topics: 2
    Replies: 2
    Points: 32
    Rank: Member

    @postanote. Many thanks, I knew I must be doing something silly!

  • #161784

    Participant
    Topics: 2
    Replies: 999
    Points: 1,946
    Helping Hand
    Rank: Community Hero

    No worries.

The topic ‘Problem adding element to XML file’ is closed to new replies.