XML : Add a node into .config file [duplicate]

This question already has an answer here:

I have the following .config file.

  <?xml version="1.0" encoding="utf-8"?>  <packages>    <package id="package1" version="123" targetFramework="net40" />    <package id="package2" version="123" targetFramework="net40" />    <package id="package3" version="123" targetFramework="net40" />  </packages>    

I also have a hashmap with the package names. I am trying to iterate through the hashmap keys and add them into the .config file. For example, if my hashmap has package4 and package5, I would like my .config file to look something like below

  <?xml version="1.0" encoding="utf-8"?>  <packages>    <package id="package1" version="123" targetFramework="net40" />    <package id="package2" version="123" targetFramework="net40" />    <package id="package3" version="123" targetFramework="net40" />    <package id="package4" />    <package id="package5" />  </packages>    

Below is my PowerShell code, this saves the .config but doesn't have the package4 and package5 in it.

  $configfile = ('samplepackage.config')  $config = New-Object XML  $config.Load($configfile)    foreach ($key in $tempHashmap.GetEnumerator()) {     $obj = $config.packages.package     $obj.id = $key.Name  }    $config.Save($configfile)    

I am not sure how this could be done. Can someone please help me out?

No comments:

Post a Comment