2015-05-12 73 views
1

我正在阅读使用PowerShell的XML文件,我需要提取特定标签的值,但我希望能够在变量中包含该标签。指定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的值作为字符串输出,它是空白的。

回答

1

$XMLinfoXPath 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 
+0

这个工程就像一个魅力,谢谢你veyr多大的帮助,真的赞赏。 –

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 }