3

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.

1 Answer 1

3

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)
        {
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Robert that worked a treat. I have marked your answer correct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.