Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell: How to use Format-Table with XML data

<tickets type="array">
    <ticket>
        <assigned-user-id type="integer">123</assigned-user-id>
        <closed type="boolean">true</closed>
        <creator-id type="integer">177522</creator-id>
        <number type="integer">306</number>
        <state>resolved</state>
        <tag nil="true"/>
        <title>
        title text 1
        </title>
        <updated-at type="datetime">2012-03-14T13:13:11+11:00</updated-at>
        <user-id type="integer">96438</user-id>
        <version type="integer">3</version>
        <user-name>Username</user-name>
    </ticket>
</tickets>

I am a Powershell newbie and find a question on xml and format-table. Given above xml file. If I run below script to display tickets in a table, the value of "number", "closed" could not be shown

$t = [xml](new-object system.net.webclient).downloadstring($xmlfilepath)
$t.tickets.ticket | Format-Table -Property title, state, user-name, url, number, closed

Return:

title            state       user-name       number       closed                                      
-----            -----       ---------       ------       ------                                
title text 1     resolved    Username        number       closed   
title text 2     resolved    Username        number       closed   

Is it the only way I have to use foreach and selectSingleNode("ticket").get_InnerXml() to get all the values?

Thank you.

like image 479
seanbun Avatar asked Mar 21 '12 02:03

seanbun


People also ask

How do you display data in a table format in XML?

If your data or table contains an XML column or LOB data, you must use the DATAFORMAT=XML clause on the EXPORT DATA or EXPORT TABLE command. This format can also be used when the data or table to be exported does not contain an XML column or LOB data.

How do I display XML in PowerShell?

One way to read an XML document in PowerShell is to typecast a variable to the type [xml]. To create this variable, we can use the Get-Content cmdlet to read all of the text in an XML document. To typecast the output of Get-Content we can simply prepend the text [xml] before the variable.

Can PowerShell parse XML?

Another way to use PowerShell to parse XML is to convert that XML to objects. The easiest way to do this is with the [xml] type accelerator. By prefixing the variable names with [xml] , PowerShell converts the original plain text XML into objects you can then work with.

Which PowerShell command does support XML?

The Select-Xml cmdlet lets you use XPath queries to search for text in XML strings and documents. Enter an XPath query, and use the Content, Path, or Xml parameter to specify the XML to be searched.


1 Answers

If you notice those nodes have attributes so you will need to get to the data of the node. try the following:

$t.tickets.ticket | Format-Table -AutoSize -Property title, state, user-name, url,
@{Label="number"; Expression={$_.number."#text"}},
@{Label="closed"; Expression={$_.closed."#text"}}
like image 86
Norman Skinner Avatar answered Oct 03 '22 01:10

Norman Skinner