如何使用 PowerShell 更新 XML 节点的值? [英] How can I update the value for a XML node using PowerShell?

查看:39
本文介绍了如何使用 PowerShell 更新 XML 节点的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何更改节点的值Test功率?

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

示例:

<?xml version="1.0"?>
<configuration>
    <appSettings>
        <add key="DeploymentDate" value="test" />
        <add key="DateOfInitialization" value="Vinoj" />
    </appSettings>
    <Test>Test</Test>
</configuration>

这是我目前使用的 PowerShell 脚本:

Here's the PowerShell script I currently use:

$configuration = "app.config"
[xml]$xml = New-Object XML
$xml.Load($configuration)
$xml.selectnodes("/configuration/Test") = {"UST"}
$xml.Save($configuration)

推荐答案

我不知道你到底想达到什么目的,但这个例子应该给你和想法:

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)

您不需要将 .NET 用于 xpath 查询.继续使用 PowerShell(使用 Select-Xml).
通过 Get-Content 加载 xml 文件并将其转换为 [xml] 以创建 XmlDocument 并加载文件内容也很常见.

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.

这篇关于如何使用 PowerShell 更新 XML 节点的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆