【问题标题】:How to change the value of XML Element attribute using PowerShell?如何使用 PowerShell 更改 XML Element 属性的值?
【发布时间】:2014-09-01 00:00:23
【问题描述】:

我正在尝试从 XML 标记访问和更改特定属性

XML:

<office>
  <staff branch="Hanover" Type="sales">
    <employee>
        <Name>Tobias Weltner</Name>
        <function>management</function>
        <age>39</age>
    </employee>
    <employee>
        <Name>Cofi Heidecke</Name>
        <function>security</function>
        <age>4</age>
    </employee>
  </staff>
  <staff branch="London" Type="Technology">
   <employee>
    <Name>XXXX</Name>
    <function>gement</function>
    <age>39</age>

从上面的例子中,我想打印分支属性,然后想在整个 XML 中用一个值来改变它,比如纽约,并使用下面的代码来做到这一点

       $xml=New-Object XML

      $xml.Load("C:\FE6Work.xml")

      $node=$xml.SelectNodes("/office/staff")

      write-output $node.branch
      $node.branch="New York"

但是得到一个错误,指出找不到元素。

有人可以帮忙吗?

【问题讨论】:

标签: xml powershell powershell-2.0


【解决方案1】:

尝试以下方法:

$nodes = $xml.SelectNodes("/office/staff");
foreach($node in $nodes) {
    $node.SetAttribute("branch", "New York");
}

这将遍历 SelectNodes() 返回的所有节点并修改每个节点。

【讨论】:

  • @user3759904:您输入的是“setAttribute”还是“SetAttribute”(应该是后者,区分大小写)?如果您确实使用正确的大小写键入了它,究竟会发生什么(例如,您收到错误或属性没有改变)?
  • 方法调用失败,因为 [System.Xml.XPathNodeList] 不包含名为“SetAttribute”的方法。
  • @user3759904:您可能使用的是不同的 PowerShell 版本,我已经使用 4.0 进行了测试,它使用您的代码和输入可以完美运行。无论如何,我已经用更长的版本编辑了我的答案,这也应该适用于早期的 PowerShell 版本。
  • 老兄,这太棒了!我编写了这个花哨的大 UI,并认为我不得不放弃整个事情,因为我不知道如何更改 XML 值。 +1,太棒了,你摇滚。
  • @PeterK,我无法编辑您的帖子(编辑太短),分支字符串末尾缺少双引号。
【解决方案2】:

您可以像这样直接访问[xml] 对象中的属性:

# C:\temp> $xml = [xml](Get-Content C:\FE6Work.xml)
# C:\temp> $xml.office.staff

branch                   Type                           employee                                                             
------                   ----                           --------                                                             
Hanover                  sales                          {Tobias Weltner, Cofi Heidecke}                                      
London                   Technology                     {XXXX, Cofi}                                                         

# C:\temp> $xml.office.staff | foreach{$_.branch = "New York"}
# C:\temp> $xml.office.staff

branch                   Type                           employee                                                             
------                   ----                           --------                                                             
New York                 sales                          {Tobias Weltner, Cofi Heidecke}                                      
New York                 Technology                     {XXXX, Cofi}                                                         

【讨论】:

  • 感谢您对此的帮助,在 xml.Save($path) 上出现以下错误,对此的任何想法都会非常有帮助 为“Save”和参数计数找到多个模棱两可的重载:“1 "。
  • 忽略它的排序
  • 当我更新文件时,它在 xml 中写入空白值,例如分支设置为“”,更新的代码是 $folders=$xml.office.Staff | Select-Object{ $FolderNameInitial + $_.branch.Substring(4)} $nodes=$xml.SelectNodes("/office/staff") foreach($nodes in $nodes) { if($node -eq $null) { 写入输出“找不到属性/属性”} else { $node.SetAttribute("branch",$folders) } } $xml.Save($path)
【解决方案3】:

如果我们从控制台获取属性并改变它的值?

$path=Read-Host -Prompt 'Enter path of xml file'
[xml]$xmldata = get-content "$path"

$tag = Read-Host -Prompt 'Enter tag'
$value = Read-Host -Prompt 'Enter value'
$xmldata.InstallConfig.$tag="$value"
$xmldata.Save($path)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-03
    • 2010-10-02
    • 2021-08-28
    • 2020-05-30
    • 2021-12-24
    • 2017-05-18
    相关资源
    最近更新 更多