gpt4 book ai didi

.net - 使用 PowerShell 解析 XML 文件

转载 作者:行者123 更新时间:2023-12-04 09:28:11 28 4
gpt4 key购买 nike

我的应用程序之一正在生成以下 XML 文件。

<root>
<command name="Set">
<property name="PWR.WakeupOnLAN" value="6" errorcode="0x0"/>
</command>
<command name="Set">
</command>
<command name="biossettings">
<property name="task" value="Succeeded." errorcode="0x0"/>
</command>
</root>

我有兴趣阅读“PWR.WakeupOnLAN”属性名称的值和错误代码。在这里发帖之前,我尝试了各种方法,但找不到在 powershell 中读取属性的正确代码。任何人都可以帮助我使用powershell代码吗?

最佳答案

在 PowerShell 2.0 中,您可以使用新的 Select-Xml 解决这个问题。 cmdlet 和 XPath表达:

[xml]$document = "<root><command name='Set'><property name='PWR.WakeupOnLAN' value='6' errorcode='0x0'/></command><command name='Set'></command><command name='biossettings'><property name='task' value='Succeeded.' errorcode='0x0'/></command>"

$value = (Select-Xml -Xpath "//property[@name='PWR.WakeupOnLAN']/@value" $document).Node.Value
$errorCode = (Select-Xml -Xpath "//property[@name='PWR.WakeupOnLAN']/@errorcode" $document).Node.Value

相关资源:
  • Select-Xml
  • Processing XML with PowerShell
  • XmlDocument Class
  • XmlAttribute Class
  • 关于.net - 使用 PowerShell 解析 XML 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5933719/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com