Salah K.
Salah K.

Reputation: 143

Getting a value of a node in an XML file

I'm trying to write a script that will get the value of a node in multiple XML files.

Here is the XML structure :

<Report>
    <ReportSections>
        <ReportSection>
            <Body>
                <ReportItems>
                    <Textbox Name="lbReportName">
                        <Paragraphs>
                            <Paragraph>
                                <TextRuns>
                                    <TextRun>
                                        <Value>Activity Report</Value>
                                    </TextRun>
                                </TextRuns>
                            </Paragraph>
                        </Paragraphs>
                    </Textbox>
                </ReportItems>
            </Body>
        </ReportSection>
    </ReportSections>
</Report>

I use this script to search through the XML :

Select-XML -Path "N:\TEMP\XML\0.xml" –Xpath "//*[@Name='lbReportName']" 

(Because the structure is not the same above the name "lbReportName").

Now, how can I get the value "Activity Report" ?

(After the name "lbReportName", the structure is the same for all XML files)

Upvotes: 1

Views: 89

Answers (1)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174990

After the name "lbReportName", the structure is the same for all XML files

That makes it easier - since you already have a wildcard selector with an appropriate clause for the "common ancestor", it's as easy as just describing the rest of the path down to the <Value> nodes:

$ValueNodes = Select-Xml ... "//*[@Name='lbReportName']/Paragraphs/Paragraph/TextRuns/TextRun/Value"

# Let PowerShell's property enumeration behavior tackle the rest
$Values = $ValueNodes.Node.InnerText

Upvotes: 1

Related Questions