Inserting xml nodes

Welcome Forums General PowerShell Q&A Inserting xml nodes

This topic contains 0 replies, has 1 voice, and was last updated by  Forums Archives 7 years ago.

  • Author
  • #5834

    Points: 0
    Rank: Member

    by Ritmo2k at 2013-04-22 08:36:23

    I have some xml I parse out and need to insert a default node into if it does not already exist:

    [XML]$xml = Get-Content 'C:\path\file.xml'

    $def_node = Select-Xml -Xml $xml -XPath "/root/def_node"
    $def_node_xml = $def_node.get_Node()

    Foreach ($element in (Select-Xml -Xml $xml -XPath $query))
    $foo_xml = $element.get_Node()
    # Insert $def_node_xml into an element named 'test_node'.

    I am not clear on how to go about this, I assumed I had to CreateElement on $foo_xml then AppendChild($def_node_xml) on it but that doesnt work.
    Any ideas?


    by Ritmo2k at 2013-04-22 17:53:10

    Solved this, you can still manipulate the System.Xml.XmlNode object ($xml ) while in the foreach loop where $query_new
    is a derived query applicable to the current $element you are iterating over.


The topic ‘Inserting xml nodes’ is closed to new replies.