![](/img/trans.png)
[英]PowerShell XML - Get value from node depending on another nodes value
[英]How to get the node value from xml using PowerShell script?
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="MultipleSuite">
<suite-files>
<suite-file path="samplexml_1.xml"></suite-file>
</suite-files>
</suite>
<!-- Suite -->
这里如何获得'samplexml_1.xml'名称?
使用XPath和Select-Xml
cmdlet :
$suite_file = Select-Xml "//suite-file" yourfile.xml
这将选择一个<suite-file>
节点数组(在您的示例中,只有一个匹配项)。 您可以像这样访问结果:
$suite_file.Node.path
版画
samplexml_1.xml
将xml文件的内容获取为[xml]
类型变量。 像这样:
[xml]$xml = Get-Content file.xml
$xml.suite.'suite-files'.'suite-file'.path
#samplexml_1.xml
这将起作用,但根据最终要实现的目标,可能不是最佳的解决方案。
$xml = [xml]'<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="MultipleSuite">
<suite-files>
<suite-file path="samplexml_1.xml"></suite-file>
</suite-files>
</suite>'
$xml.suite."suite-files"."suite-file".path
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.