Quantcast
Channel: Active questions tagged config - Stack Overflow
Viewing all articles
Browse latest Browse all 5060

Add sub elements to a config file (XML) with powershell

$
0
0

i'm getting crazy. I need to modify this XML file in powershell

<system.diagnostics><trace autoflush="attr1" indentsize="attr2"><listeners><add name="attr1" type="attr2" initializeData="attr3" /><remove name="Default" /></listeners></trace><switches><add name="attr1" value="attr2" /> </switches></system.diagnostics>

i need to add a new element after the "remove" one: i tried to use the following code

$fileXML = 'C:\Users\XXXX\file.config'$contentXML = New-Object XML$contentXML.Load($fileXML)$elementXML = $contentXML.SelectSingleNode("//listeners")$childElementsXML = $contentXML.CreateElement("set")$elementXML.AppendChild($childElementsXML)$contentXML.DocumentElement.AppendChild($elementXML)$contentXML.Save($fileXML)

but it's moving the element "listeners" at the same level of "trace". What am i doing wrong?

<system.diagnostics><trace autoflush="attr1" indentsize="attr2"></trace><switches><add name="attr1" value="attr2" /></switches><listeners><add name="attr1" type="attr2" initializeData="attr3" /><remove name="Default" /><set /></listeners></system.diagnostics>

thanks in advance for your help guys


Viewing all articles
Browse latest Browse all 5060


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>