Home > Mobile >  Selecting value of XML tag with PowerShell
Selecting value of XML tag with PowerShell

Time:04-11

I'm pretty new to PowerShell and I'm struggling to read this XML data. The XML looks something like this:

XML

<RESULTS>
    <ROW>
        <COLUMN NAME="ATTR1"><![CDATA[123456ABCDEF]]></COLUMN>
        <COLUMN NAME="ATTR2"><![CDATA[0.0.0.0]]></COLUMN>
        <COLUMN NAME="ATTR3"><![CDATA[Hello World]]></COLUMN>
        <COLUMN NAME="ATTR4"><![CDATA[Lorem ipsum]]></COLUMN>
        <COLUMN NAME="ATTR5"><![CDATA[This is some text]]></COLUMN>
    </ROW>
</RESULTS>

This is what my PowerShell looks like so far:

PowerShell

$xmlpath = 'Path\To\XML\File.xml'

$xmldata = [xml](Get-Content $xmlpath)

$xmldata.RESULTS.ROW.COLUMN | Select | Where-Object {$_.NAME -eq "ATTR3"}

And this is the result:

Result

ATTR3 Hello World                              

I would like to just retrieve "Hello World," but my script is also retrieving the attribute name (ATTR3). What am I missing?

Example of desired result

Hello World

CodePudding user response:

For the community's reference, Santiago Squarzon's solution is provided below:

Solution

$xmldata.RESULTS.ROW.COLUMN.Where{ $_.NAME -eq "ATTR3"}.'#cdata-section'
  • Related