1

我正在尝试使用点符号来查找此 XML 中的 Webs 元素列表:

<Databases>
    <Database SiteCount="2" Name="MyDB" DataSource="BLAHDEV">
        <Site Id="72affdff-b0a9-404e-a000-08cfc1c2828a" OwnerLogin="Blah\Blah" InSiteMap="True">
            <Webs Count="1">
                <Web Id="5fb7df63-0ab3-4cb2-88de-1a94ceda0fbf" Url="/people/me/blah" LanguageId="1033" TemplateName="SPSPERS#0" TemplateId="21" />
            </Webs>
        </Site>
        <Site Id="9ae7dd88-cdd9-4d22-b6af-2185ddb22994" OwnerLogin="Blah\Blah" InSiteMap="True">
            <Webs Count="1">
                <Web Id="45d2d002-4e7d-4f22-9cd2-c93fdd49daab" Url="/people/me/blah2" LanguageId="1033" TemplateName="SPSPERS#0" TemplateId="21" />
            </Webs>
        </Site>
    </Database>
</Databases>

然后我将它加载到powershell中:

[xml]$xml = Get-Content MyXMLFile.xml

使用点符号进行查询直到 Site 元素:

PS> $xml
Databases
---------
Databases

PS> $xml.Databases
Database
--------
Database

PS> $xml.Databases.Database
SiteCount             Name              DataSource      Site
---------             ----              ----------      ----
2                     MyDB              BLAHDEV         {Site, Site}

PS> $xml.Databases.Database.Site
Id                            OwnerLogin        InSiteMap                 Webs
--                            ----------        ---------                 ----
72affdff-b0a9-404e-a000-08... Blah\Blah         True                      Webs
9ae7dd88-cdd9-4d22-b6af-21... Blah\Blah         True                      Webs

但是,当我尝试获取 Webs 元素时,没有返回任何结果:

PS> $xml.Databases.Database.Site.Webs
PS>

我找不到 Webs 元素有什么原因吗?是因为有多个站点元素还是其他原因?

4

1 回答 1

4

$xml.Databases.Database.Site is a Collection, you'll need to pipeline it:

$xml.Databases.Database.Site | Select -Expand Webs

Or use xpath (edit: SelectNodes is for each element, not the collection)

...Site | ForEach { $_.SelectNodes('Webs') } #or something similar

Or loop it

...Site | ForEach { $_.Webs } 
于 2013-07-18T23:10:38.620 回答