我正在使用PowerShell读取XML文件,我需要提取特定标记的值,但我希望能够在变量中包含该标记。
如果我这样做:
[xml]$content = Get-Content "$($splitsinglehost[1])$XPfilelocation$XMLfiletoRead"
$XMLvalue= $content.BT9000_XML_FILE.Config_SETX.I_Camera_Manufacturer
它有效,我正在寻找的值作为字符串存储在$XMLvalue
中。
但如果我这样做:
$XMLinfo = "BT9000_XML_FILE.Config_SETX.I_Security_Module_Manufacturer"
[xml]$content = Get-Content "$($splitsinglehost[1])$XPfilelocation$XMLfiletoRead"
$XMLvalue= $content.$XMLinfo
它没有用。如果我尝试将$XMLvalue
的值输出为字符串,则为空白。
答案 0 :(得分:1)
将$XMLinfo
设为XPath expression并使用SelectSingleNode()
选择节点:
$XMLinfo = '/BT9000_XML_FILE/Config_SETX/I_Security_Module_Manufacturer'
[xml]$content = Get-Content 'C:\path\to\your.xml'
$XMLvalue = $content.SelectSingleNode($XMLinfo).innerText
答案 1 :(得分:0)
所以我不知道你是否可以按照你试图使用它的方式让它工作。
你可以这样做:
$XMLinfo = "BT9000_XML_FILE.Config_SETX.I_Security_Module_Manufacturer"
[xml]$content = Get-Content "$($splitsinglehost[1])$XPfilelocation$XMLfiletoRead"
$XMLinfo = $XMLinfo.Replace('.','/')
$XMLvalue= $content.SelectNodes($XMLinfo) | % { $_.innerText }