adding XML sub-elements

后端 未结 3 1208
鱼传尺愫
鱼传尺愫 2020-12-03 04:33

With PowerShell, I want to add several sub-elements into an XML tree.
I know to ADD ONE element, I know to add one or several attributes, but I don\'t understand how to

3条回答
  •  误落风尘
    2020-12-03 05:29

    I prefer creating xml by hand, instead of using API to construct it node by node, as imho by hand it will be much more readable and more maintable.

    Here is an example:

    $pathToConfig = $env:windir + "\Microsoft.NET\Framework64\v4.0.30319\Config\web.config"
    
    $xml = [xml] (type $pathToConfig)
    
    [xml]$appSettingsXml = @"
    
        
    
    "@
    
    
    $xml.configuration.AppendChild($xml.ImportNode($appSettingsXml.appSettings, $true))
    $xml.Save($pathToConfig)
    

提交回复
热议问题