【问题标题】:Fetching an attribute value from XPath of an XML using powershell使用 powershell 从 XML 的 XPath 中获取属性值
【发布时间】:2015-12-16 17:05:39
【问题描述】:

我在使用 powershell 从 XML 配置文件的 XPath 获取和设置属性值时遇到问题。 属性就像

"<add key="TransactDeadLetterQueueThreshold" value="0" />".

示例 XML 文件:

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <appSettings>
        <add key="ReportFile" value="C:\Program Files\Fusion\Viewer Suite Monitoring\ViewersMonitoring\monitor.txt" />
    <add key="QueueThreshold" value="1000" />
    <add key="TransactDeadLetterQueueThreshold" value="0" />
    <add key="TimeCardsThreshold" value="6000" />
    <add key="PercentDiskSpaceThreshold" value="10" />
    <add key="QueueCreatedFormat" value="The {0} queue is created." />
    <add key="QueueOverThresholdFormat" value="The {0} queue size is larger than {1} messages." />
    <add key="QueueTransactDeadLetterOverThresholdFormat" value="The transactional dead queue size is larger than {0} messages." />
    <add key="DriveThresholdFormat" value="The {0} drive space is less than {1}." />
    <add key="QueueRemovedFormat" value="The {0} queue is removed." />
    <add key="TimeCardsOverThresholdFormat" value="The time card records count is greater than or equal to {0} ." />
    <add key="FailedConnectDbFormat" value="Failed: unable to access the database, reason: {0}" />
    <add key="LogName" value="Viewer Suite Monitor" />
    <add key="ViewersPrivateQueues" value="private$\adt,private$\infusionviewer,private$\timecards" />
    <add key="DrivetobeMonitored" value="D:" />
  </appSettings>

我尝试通过以下方式获取属性:

$xdoc = Get-content "C:\Program Files\Fusion\Viewer Suite Monitoring\CareFusion.Infusion.ViewerSuite.Monitoring.exe.config"


$value = ($xdoc.configuration.appSettings.SelectSingleNode(add[@key = "PercentDiskSpaceThreshold"])) + $value.GetAttribute('key')

也尝试过这种方式:

$xdoc = Get-Content "C:\Program Files\Fusion\Viewer Suite Monitoring\Fusion.ViewerSuite.Monitoring.exe.config"
$value = $xdoc.selectSingleNode("/configuration/appSettings/add[@key = ""TransactDeadLetterQueueThreshold""] ")'+"
$value.GetAttribute('key')"

我遇到了错误。我不知道这有什么问题。 请帮忙。

【问题讨论】:

标签: xml powershell xpath


【解决方案1】:

你在正确的轨道上。

确保 PowerShell 将 XML 视为具有 [xml] 类型加速器的实际 XmlDocument 对象:

$XmlPath = '.\path\to\app.config'
$XDoc = [xml](Get-Content $XmlPath)

选择要更改其值的节点:

$TransactDeadLetterNode = $XDoc.SelectSingleNode('//add[@key = "TransactDeadLetterQueueThreshold"]')

设置属性:

$TransactDeadLetterNode.SetAttribute("value",1)

(对要更改的任何其他节点重复此操作)
最后保存文档:

$XDoc.Save($XmlPath)

【讨论】:

    【解决方案2】:

    这是一个更多依赖 PowerShell 而更少依赖 .NET XML API 的替代方案:

    # Get the attribute value
    $xdoc = [xml](Get-Content <path-to-xml-file>)
    $key = $xml.configuration.appsettings.add | Where {$_.Key -eq 'TransactDeadLetterQueueThreshold'}
    $key.Value
    
    # Set the attribute
    $key.Value = '1'
    

    您仍然需要使用$xdoc.Save(&lt;path&gt;) 将更改保存到文件。

    【讨论】:

    • 我的意图是最初尝试获取属性值并使用所需的值更新属性值。我已经尝试了解决方案,但是抛出了一个错误,这个异常:+ $key = $xml .configuration.appSettings.add | where
    • 当您发布有关 PowerShell 的问题时,请务必添加您使用的 PowerShell 版本作为标签。你是v2吗?如果是这样,请尝试更新的答案。
    • 神奇 - 非常感谢基思。这非常有效。此代码适用于 v1.0 和 v2.0。
    猜你喜欢
    • 2012-08-26
    • 1970-01-01
    • 1970-01-01
    • 2013-11-15
    • 1970-01-01
    • 1970-01-01
    • 2021-10-04
    • 2015-03-15
    相关资源
    最近更新 更多