简体   繁体   中英

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 -->

here how to get the 'samplexml_1.xml' name ?

Use XPath and the Select-Xml cmdlet :

$suite_file = Select-Xml "//suite-file" yourfile.xml

This selects an array of <suite-file> nodes (in your example, there is only one match). You can access the result like this:

$suite_file.Node.path

prints

samplexml_1.xml

Get content of xml file to [xml] type variable. Like so:

[xml]$xml = Get-Content file.xml
$xml.suite.'suite-files'.'suite-file'.path
#samplexml_1.xml

This would work but is probably not the best solution depending on what you want to achieve at the end.

$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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM