参考资料:
https://www.red-gate.com/simple-talk/sysadmin/powershell/powershell-data-basics-xml/
以及:
https://stackoverflow.com/a/65264118/4531180
如何打印元素及其属性的列表?
PS /home/nicholas/powershell>
PS /home/nicholas/powershell> $doc = new-object System.Xml.XmlDocument
PS /home/nicholas/powershell> $file = resolve-path('./bookstore.xml')
PS /home/nicholas/powershell> $doc.load($file)
PS /home/nicholas/powershell>
PS /home/nicholas/powershell> $doc.bookstore.book[1].author.first-name
ParserError:
Line |
1 | $doc.bookstore.book[1].author.first-name
| ~~~~~
| Unexpected token '-name' in expression or statement.
PS /home/nicholas/powershell>
PS /home/nicholas/powershell> $doc.bookstore.book[1].author
first-name last-name
---------- ---------
Margaret Atwood
PS /home/nicholas/powershell>
PS /home/nicholas/powershell> $doc.bookstore
bk book
-- ----
urn:samples {book, book, book, book}
PS /home/nicholas/powershell> 发布于 2020-12-12 19:00:59
来解决 for-display格式设置问题:
如果仔细查看示例输出从你自己的回答,您会发现尽管显示非常有用,但author属性的值是author,而不是显示(假定的) first-name和last-name子元素值。
问题是,PowerShell的默认输出格式表示具有的子元素
由元素的名称命名,仅.
尤其是具有深嵌套元素的output.,这会导致无帮助的
Workarounds,可能结合在一起:
- This will likely only be helpful with perhaps at most another level of nesting will be visually helpful, given that the XML text is a _single-line_ representation that is _not_ pretty-printed.- You can **pipe** **`.OuterXml`** **/** **`InnerXml`** **values to a** _**pretty-printing**_ **function**, which **requires some extra work**, however, because no such functionality is directly exposed by PowerShell.Format-* cmdlet (如Format-Table)和计算性质。- While this allows you full control over what is displayed, it is more work.见下面的例子。
# Sample XML document
$xmlDoc = [xml] @"
<?xml version="1.0"?>
<bookstore>
<book id="bk101">
<author>
<first-name>Matthew</first-name>
<last-name>Gambardella</last-name>
</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications with XML.</description>
</book>
<book id="bk102">
<author>
<first-name>Kim</first-name>
<last-name>Rall</last-name>
</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
<description>A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world.</description>
</book>
</bookstore>
"@要获得所有<book>元素的有用表示,包括<author>子元素的<first-name>和<last-name__>子元素,通过__>和计算的属性获取
$xmldoc.bookstore.book | Select-Object id,
@{ n='author'; e={ $_.author.'first-name' + ' ' + $_.author.'last-name'} },
title, genre, price, publish_date, description这将产生(注意author属性现在是如何列出名字和姓氏的):
id : bk101
author : Matthew Gambardella
title : XML Developer's Guide
genre : Computer
price : 44.95
publish_date : 2000-10-01
description : An in-depth look at creating applications with XML.
id : bk102
author : Kim Rall
title : Midnight Rain
genre : Fantasy
price : 5.95
publish_date : 2000-12-16
description : A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world.要通过一个辅助<book>实例通过漂亮打印的XML获得所有System.Xml.Linq.XDocument元素的有用表示:
# Load the assembly that contains XDocument.
# Note: Required in Windows PowerShell only, and only once per session.
Add-Type -AssemblyName System.Xml.Linq
$xmldoc.bookstore.book | ForEach-Object {
([System.Xml.Linq.XDocument] $_.OuterXml).ToString()
}这产生了(一种打印得很好的XML表示):
<book id="bk101">
<author>
<first-name>Matthew</first-name>
<last-name>Gambardella</last-name>
</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications with XML.</description>
</book>
<book id="bk102">
<author>
<first-name>Kim</first-name>
<last-name>Rall</last-name>
</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
<description>A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world.</description>
</book>请注意,您可以将格式化代码包装在一个名为Format-Xml的简单(过滤器)函数中,您可以将该函数放在$PROFILE文件中(在Windows中,也可以将Add-Type -AssemblyName System.Xml.Linq放在该文件的上方):
filter Format-Xml { ([System.Xml.Linq.XDocument] $_.OuterXml).ToString() }现在,格式设置非常简单,如:
$xmldoc.bookstore.book | Format-Xml发布于 2020-12-12 13:03:45
只是不使用books
PS /home/nicholas/powershell>
PS /home/nicholas/powershell> $doc.bookstore.book
genre : novel
publicationdate : 1997
ISBN : 1-861001-57-8
title : Pride And Prejudice
author : author
price : 24.95
genre : novel
publicationdate : 1992
ISBN : 1-861002-30-1
title : The Handmaid's Tale
author : author
price : 29.95
genre : novel
publicationdate : 1991
ISBN : 1-861001-57-6
title : Emma
author : author
price : 19.95
genre : novel
publicationdate : 1982
ISBN : 1-861001-45-3
title : Sense and Sensibility
author : author
price : 19.95
PS /home/nicholas/powershell> 哇哦。
https://stackoverflow.com/questions/65264796
复制相似问题