我有以下xml
下面的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("Site")
找不到站点列表。我如何找到这些。
最佳答案
原因是您在之前的行中使用了 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)
{
}
}
}
关于xml - 嵌套的 XML Powershell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21432353/