1

我正在尝试编写一个脚本来获取多个 XML 文件中节点的值。

这是 XML 结构:

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

我使用这个脚本来搜索 XML:

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

(因为名称“lbReportName”上面的结构不一样)。

现在,我怎样才能获得“活动报告”的价值?

(名称“lbReportName”之后,所有 XML 文件的结构都相同)

4

1 回答 1

1

在名称“lbReportName”之后,所有 XML 文件的结构都相同

这使它更容易 - 因为您已经有了一个带有“共同祖先”的适当子句的通配符选择器,它就像描述到<Value>节点的其余路径一样简单:

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

# Let PowerShell's property enumeration behavior tackle the rest
$Values = $ValueNodes.Node.InnerText
于 2020-09-28T10:58:28.430 回答