Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested XML Powershell

Tags:

powershell

xml

I have the following xml

enter image description here

The following powershell is reading the xml

write-host "`nParsing file SharepointSetUpData.xml`n"
[System.Xml.XmlDocument] $xd = new-object System.Xml.XmlDocument
$file = resolve-path("SharepointSetUpData.xml")
$xd.load($file)
#$nodelist = $xd.selectnodes("/testCases/testCase") # XPath is case sensitive
$nodelist = $xd.selectnodes("/WebApplications/WebApplication") # XPath is case sensitive
foreach ($testCaseNode in $nodelist) {
  $WebAppid = $testCaseNode.getAttribute("id")
  $inputsNode = $testCaseNode.selectSingleNode("SiteCollections")
  $SiteCollection = $inputsNode.selectSingleNode("SiteCollection").get_InnerXml()
  $siteslist = $SiteCollection.selectnodes("Site")
  $optional = $inputsNode.selectSingleNode("SiteCollection").getAttribute("url")
  $arg2 = $inputsNode.selectSingleNode("arg2").get_InnerXml()
  $expected = $testCaseNode.selectSingleNode("expected").get_innerXml()
  #$expected = $testCaseNode.expected 
  write-host "WebApp ID = $WebAppid SiteCollection = $SiteCollection Optional = $optional Arg2 = $arg2 Expected value = $expected"
}

The problem is the line

$siteslist = $SiteCollection.selectnodes("Site")

The site lists can not be found. How do I find those.

like image 660
vinnie Avatar asked Feb 19 '26 03:02

vinnie


1 Answers

The reason is that you're using the InnerXml property on the line before, which returns a string. If you just remove the get_InnerXml() call on the line above it should work just fine. On the other hand, you could work with a simpler syntax in PowerShell, if you want to. Just as a sample of the different syntax:

[xml]$xml = Get-Content .\data.xml
$webApplications = $xml.WebApplications.WebApplication
foreach ($application in $webApplications)
{
    $webAppId = $application.id
    $expected = $application.expected
    foreach ($siteCollection in $application.SiteCollections.SiteCollection)
    {
        foreach($site in $siteCollection.Site)
        {
        }
    }
}
like image 137
Robert Westerlund Avatar answered Feb 20 '26 17:02

Robert Westerlund



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!