首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >嵌套XML Powershell

嵌套XML Powershell
EN

Stack Overflow用户
提问于 2014-01-29 13:21:19
回答 1查看 2.7K关注 0票数 3

我有以下xml

下面的powershell正在读取xml

代码语言:javascript
复制
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"
}

问题是这条线

$SiteCollection.selectnodes("Site") $siteslist =

无法找到网站列表。我怎么找到那些。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-01-29 15:50:30

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

代码语言:javascript
复制
[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)
        {
        }
    }
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21432353

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档