Powershell:如何在XML数据中使用Format-Table

时间:2023-01-30 00:38:19
<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

我是一个Powershell新手,在xml和format-table上找到了一个问题。上面给出的xml文件。如果我在脚本下面运行,以在表中显示票据,则无法显示“number”、“closed”的值

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

这是我必须使用foreach和selectSingleNode(“ticket”).get_InnerXml()来获得所有值的唯一方法吗?

Thank you.

谢谢你!

1 个解决方案

#1


6  

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

#1


6  

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