Reputation: 2965
This is probably simple, but I'm trying to determine if a node exists in an XML document. I thought I found an answer in this post, How to check whether a node exists or not using powershell without getting exception?, but I didn't get it to work. This is my latest attempt.
foreach ($vendor in $xml.Vendors.Vendor| Where-Object {$_.Type -match "Send"}) {
$NodeExists = $vendor.SelectSingleNode($vendor.EncKey)
if ($NodeExists -ne $null) {
# Do something
}
else {
# something else
}
}
Here is XML from my test file. I need to find out if EncKey exists or not for each vendor.
<?xml version="1.0" encoding="UTF-8"?>
<!-- Vendors we will send and retreive files from Get-Send means we will get a file and send them a file Send means we will only send them a file-->
<Vendors>
<Vendor Type="Get-Send">
<Name>Vendor1</Name>
<RemotePath>/Remote/Path1/</RemotePath>
<EncKey>pgpenc.key</EncKey>
</Vendor>
<Vendor Type="Send">
<Name>Vendor2</Name>
<RemotePath>/Remote/Path2/</RemotePath>
<!-- This one has no EncKey -->
</Vendor>
</Vendors>
Upvotes: 20
Views: 58335
Reputation: 1035
Provided you load the $xml object as an XmlDocument using
$xml = new-object System.Xml.XmlDocument
$xml.LoadXml( (get-content $pathToXmlFile) )
Then you can do this, which is much simpler:
if ($vendor.encKey -ne $null) {
# does exist
} else {
# does not exist
}
Upvotes: 5
Reputation: 2965
It appears that I was using the wrong syntax for SelectSingleNode. Here is a working example.
[xml]$xml = @'
<?xml version="1.0" encoding="UTF-8"?>
<!-- Vendors we will send and retreive files from Get-Send means we will get a file and send them a file Send means we will only send them a file-->
<Vendors>
<Vendor Type="Get-Send">
<Name>Vendor1</Name>
<RemotePath>/Remote/Path1/</RemotePath>
<EncKey>pgpenc.key</EncKey>
</Vendor>
<Vendor Type="Send">
<Name>Vendor2</Name>
<RemotePath>/Remote/Path2/</RemotePath>
<!-- This one has no EncKey -->
</Vendor>
</Vendors>
'@
foreach ($vendor in $xml.Vendors.Vendor| Where-Object {$_.Type -match "Send"}) {
$NodeExists = $vendor.SelectSingleNode("./EncKey")
if ($NodeExists -ne $null) {
write-host "EncKey is null"
}
else {
write-host "EncKey is not null"
}
}
EncKey is null
EncKey is not null
Thanks everyone for your help.
Upvotes: 10
Reputation: 912
The easiest way I can think of is to try to write the node value to a variable, and then to see if that variable is null. Here's an example with the standard bookstore xml file.
[xml]$bookstore = Get-Content .\bookstore.xml
foreach ($book in $bookstore.bookstore.book | Where-Object {$_.Type -match "novel"}) {
$NodeExists = $book.author
if($NodeExists){
Write-Host $book.author
}
else{
Write-Host 'No Author'
}
}
So for your script, I would think it might be
$NodeExists = $null
foreach ($vendor in $xml.Vendors.Vendor| Where-Object {$_.Type -match "Send"}) {
$NodeExists = $vendor.EncKey
if ($NodeExists) {
# Do something
}
else {
# something else
}
}
Upvotes: 10
Reputation: 24081
Use XPath to select matching nodes. InnerText is searchable by text()
. You can use where-object
, or ?
too; behaviour is a bit different. Without sample XML it's hard to be more precise. Use XPath like so,
[xml]$doc = @'
<root>
<Vendors>
<Vendor>
<Type>Send</Type>
</Vendor>
<Vendor>
<Type>Receive</Type>
</Vendor>
</Vendors>
</root>
'@
# Xpath query will return a NodeList, even if no match is found
$node1 = $doc.SelectNodes("//Vendor/Type[text() = 'Send']")
$node2 = $doc.SelectNodes("//Vendor/Type[text() = 'Sent']")
$node1.Count
1
$node2.Count
0
# where-object will return $null if no match is found
$node1 = $doc.SelectNodes("//Vendor/Type") | ? { $_.InnerText -eq "Send" }
$node2 = $doc.SelectNodes("//Vendor/Type") | ? { $_.InnerText -eq "Sent" }
$node1 -eq $null
False
$node2 -eq $null
True
Upvotes: 4