How can I update the value for a XML node using PowerShell?

前端 未结 1 1461
春和景丽
春和景丽 2020-12-16 16:29

How can I change the value of the node Test to Power?

Example

相关标签:
1条回答
  • 2020-12-16 17:13

    I don't know what exactly you want to achieve, but the example should give you and idea:

    $file = 'c:\temp\aa\ServerService.exe.config'
    $x = [xml] (Get-Content $file)
    Select-Xml -xml $x  -XPath //root/level |
        % { $_.Node.'#text' = 'test'
            $_.Node.SomeAttribute = 'value'
          }
    $x.Save($file)
    

    You don't need to use .NET for xpath queries. Just stay with PowerShell (with Select-Xml).
    It is also common to load xml file via Get-Content and cast it to [xml] which creates XmlDocument and loads the file content.

    0 讨论(0)
提交回复
热议问题