0
votes

I have a XML document laid out like below:

<?xml version="1.0" encoding="utf-8"?>
<mailmanager xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <locations>
<store id="57fb3dc3-8716-4a71-ab5c-d8c76e20640b">
  <type>msg</type>
  <description>01038 - Wood Wharf - Fire</description>    <folder>SharePoint:https://site.domain.com/legal/01038/documents/Forms/CRMView.aspx</folder>
</store>
<store id="6873a00e-e49c-4602-af27-49d2900563d3">
  <type>msg</type>
  <description>01038 - Wood Wharf - Fire</description>
  <folder>\\site.domain.com\legal\01038\documents\</folder>
  <fileable>0</fileable>
</store>
<store id="d5e3af58-bc1d-4e45-a2dc-a9ceed803456">
  <type>msg</type>
  <description>05515 - IRELAND - Dart Underground   -  DART UNDERGROUND INTERCONNECTOR PROJECT</description>     <folder>SharePoint:https://site.domain.com/legal/05515/documents/Forms/CRMView.aspx</folder>
</store>

With thousands of store elements.

With PowerShell, is it possible to get an entire parent element (i.e. store with everything within) based on the folder value? So if folder value contains site.domain.com/legal/02323 for example, I get the parent element.

3

3 Answers

1
votes

Yes, using an XPath expression with the contains() function:

$Search = "site.domain.com/legal/01038"
$XmlDoc = [xml](Get-Content .\mydocument.xml)
$StoreNode = $xmlDoc.SelectSingleNode("//store[folder[contains(.,'$search')]]")

You could also do the above with the Select-Xml cmdlet:

$Search = "site.domain.com/legal/01038"
$StoreNode = Select-Xml -Path .\mydocument.xml -XPath "//store[folder[contains(.,'$search')]]" |Select-Object -Expand Node
0
votes

Sure, just select all stores and filter the one which folder child contain a specific value using the Where-Object cmdlet:

[xml]$content = Get-Content 'your_xml'
$content.mailmanager.locations.store | 
    Where-Object { $_.folder -eq '\\site.domain.com\legal\01038\documents\' }

Output:

id          : 6873a00e-e49c-4602-af27-49d2900563d3
type        : msg
description : 01038 - Wood Wharf - Fire
folder      : \\site.domain.com\legal\01038\documents\
fileable    : 0
0
votes

You could use XPath for this:

$document = New-Object System.Xml.XPath.XPathDocument("C:\Path\To\XmlFile.xml")
$navigator = $document.CreateNavigator()
$navigator.Select("//store[folder='SharePoint:https://site.domain.com/legal/01038/documents/Forms/CRMView.aspx']")