xml - 嵌套的 XML Powershell

标签 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("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/

相关文章:

mysql - Debian 8 上的 PowerShell - 从 mysqldump 到 gzip 的管道

Powershell如何使用仅知道文件名一部分的测试路径

java - OS X 上的应用程序包不起作用,文件寻址问题

mysql - 添加新列以在 magento CMS 页面中安装脚本

powershell - 如何使用Powershell从xml获取属性值?

powershell - invoke-expression 不会在 powershell 中抛出错误

powershell - powershell 中的延迟管道执行?

xml - 使用 Typescript 和 xml2js 解析 XML

xml - 在 Golang 中解析递归 XML

java - 通过套接字和流的 JAXB - 阅读器 block