简体   繁体   中英

Dot notation returns no results

I'm trying to use dot notation to find the list of Webs elements in this XML:

<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>

I then load it in powershell:

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

Querying using the dot notation works up until the Site element:

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

However when I try to get the Webs elements, no results are returned:

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

Is there a reason I can't find the Webs elements? Is it because there are multiple Site elements or some other reason?

$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 } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM