Search an XML Document for a Specific Value

Here is an example of how to search through an XML document for a value of an attribute:

Here is the XML file:

<?xml version="1.0" encoding="utf-8"?>
<root>
 <archive Name="20100821">
 <File Name="Log20100821.log" SizeKB="117727" LastWriteTime="08/21/2010 06:33:35" />
 <File Name="Archive20100821.log" SizeKB="3" LastWriteTime="08/21/2010 06:33:35" />
 <File Name="Routine20100821.log" SizeKB="74" LastWriteTime="08/21/2010 06:33:36" />
 </archive>
 <archive Name="20100828">
 <File Name="Log20100828.log" SizeKB="148740" LastWriteTime="08/28/2010 06:34:14" />
 <File Name="Archive20100828.log" SizeKB="4" LastWriteTime="08/28/2010 06:34:15" />
 <File Name="Routine20100828.log" SizeKB="90" LastWriteTime="08/28/2010 06:34:15" />
 </archive>
</root>

To find <archive Name=”20100828″> try this:

$xml = [xml](get-content xml.xml)
$searchItem = "20100828"
$xml.SelectNodes("root/archive") | ? { $_.Name -eq $searchItem} | % {$_.File } | ft -AutoSize

This should return these results:

Advertisement
Tagged ,
Follow

Get every new post delivered to your Inbox.