嵌套的XML Powershell

时间:2014-01-29 13:21:19

标签: xml powershell

我有以下xml

enter image description here

以下powershell正在阅读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"
}

问题在于

  

$ siteslist = $ SiteCollection.selectnodes(“网站”)

无法找到网站列表。我如何找到这些。

1 个答案:

答案 0 :(得分:3)

原因是你之前使用的是InnerXml属性,它返回一个字符串。如果您只是删除上面一行的get_InnerXml()调用,它应该可以正常工作。另一方面,如果您愿意,可以在PowerShell中使用更简单的语法。就像不同语法的示例一样:

[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)
        {
        }
    }
}