嵌套XML Powershell
我有以下xml 以下powershell正在读取xml嵌套XML Powershell,xml,powershell,Xml,Powershell,我有以下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("/testCas
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(“站点”)
找不到站点列表。如何查找这些属性。原因是您在前面的行中使用了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)
{
}
}
}
谢谢你,罗伯特,这真是个好消息。我已把你的答案标对了。