2014-01-29 72 views
2

我有以下XML嵌套XML Powershell的

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(簡稱 「網站」)

該網站列出無法找到。我如何找到這些。

回答

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) 
     { 
     } 
    } 
} 
+0

謝謝羅伯特,工作的一種享受。我已經標記你的答案是正確的。 – vinnie