如何使用ConvertTo-Xml和Select-Xml加载或读取XML文件?
我怎样才能完成这样的事情:
PS /home/nicholas/powershell>
PS /home/nicholas/powershell> $date=(Get-Date | ConvertTo-Xml)
PS /home/nicholas/powershell>
PS /home/nicholas/powershell> $date
xml Objects
--- -------
version="1.0" encoding="utf-8" Objects
PS /home/nicholas/powershell>
PS /home/nicholas/powershell> $date.OuterXml
<?xml version="1.0" encoding="utf-8"?><Objects><Object Type="System.DateTime">12/12/2020 2:43:46 AM</Object></Objects>
PS /home/nicholas/powershell>
但是,相反,读取文件?
如何加载/导入/读取/转换用于解析的xml文件using ?ConvertTo-XmlSelect-XmlXpath
PS /home/nicholas/powershell>
PS /home/nicholas/powershell> $xml=ConvertTo-Xml ./bookstore.xml
PS /home/nicholas/powershell>
PS /home/nicholas/powershell> $xml
xml Objects
--- -------
version="1.0" encoding="utf-8" Objects
PS /home/nicholas/powershell>
PS /home/nicholas/powershell> $xml.InnerXml
<?xml version="1.0" encoding="utf-8"?><Objects><Object Type="System.String">./bookstore.xml</Object></Objects>
PS /home/nicholas/powershell>
PS /home/nicholas/powershell> $xml.OuterXml
<?xml version="1.0" encoding="utf-8"?><Objects><Object Type="System.String">./bookstore.xml</Object></Objects>
PS /home/nicholas/powershell>
PS /home/nicholas/powershell> cat ./bookstore.xml
<?xml version="1.0"?>
<!-- A fragment of a book store inventory database -->
<bookstore xmlns:bk="urn:samples">
<book genre="novel" publicationdate="1997" bk:ISBN="1-861001-57-8">
<title>Pride And Prejudice</title>
<author>
<first-name>Jane</first-name>
<last-name>Austen</last-name>
</author>
<price>24.95</price>
</book>
<book genre="novel" publicationdate="1992" bk:ISBN="1-861002-30-1">
<title>The Handmaid's Tale</title>
<author>
<first-name>Margaret</first-name>
<last-name>Atwood</last-name>
</author>
<price>29.95</price>
</book>
<book genre="novel" publicationdate="1991" bk:ISBN="1-861001-57-6">
<title>Emma</title>
<author>
<first-name>Jane</first-name>
<last-name>Austen</last-name>
</author>
<price>19.95</price>
</book>
<book genre="novel" publicationdate="1982" bk:ISBN="1-861001-45-3">
<title>Sense and Sensibility</title>
<author>
<first-name>Jane</first-name>
<last-name>Austen</last-name>
</author>
<price>19.95</price>
</book>
</bookstore>
PS /home/nicholas/powershell>
xml在REPL控制台本身中创建文件按预期工作:
如何使用 Select-Xml 和 Xpath 在 Powershell 中解析 XML?
回答
在 Powershell 中正确读取 XML 文档的工作方式如下:
$doc = New-Object xml
$doc.Load( (Convert-Path bookstore.xml) )
XML 可以采用多种文件编码,使用该XmlDocument.Load方法可确保在事先不了解编码的情况下正确读取文件。
不读取具有正确编码的文件将导致数据损坏或错误,除非在非常基本或非常幸运的情况下。
由于这个原因,经常看到的使用Get-Content和转换结果字符串[xml]的方法是处理 XML 的错误方法。所以不要那样做。
您可以使用 获得正确的结果Get-Content,但这需要
- 文件编码的先验知识(例如
Get-Content bookstore.xml -Encoding UTF8) - 硬编码的文件编码到脚本(这意味着如果XML编码这将打破以往骤变)
- 将自己限制为支持的极少数文件编码
Get-Content(XML 支持更多)
这意味着您将自己置于必须手动思考和解决 XML 专门设计为自动为您处理的问题的位置。
正确地做事Get-Content会带来很多不必要的额外工作和限制。当做正确的事情很容易时,做错的事情毫无意义。
示例,加载后$doc如上图所示。
$doc.bookstore.book
打印<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
$doc.bookstore.book | Format-Table
打印与表格相同的东西
genre publicationdate ISBN title author price
----- --------------- ---- ----- ------ -----
novel 1997 1-861001-57-8 Pride And Prejudice author 24.95
novel 1992 1-861002-30-1 The Handmaid's Tale author 29.95
novel 1991 1-861001-57-6 Emma author 19.95
novel 1982 1-861001-45-3 Sense and Sensibility author 19.95
$doc.bookstore.book | Where-Object publicationdate -lt 1992 | Format-Table
过滤数据
genre publicationdate ISBN title author price
----- --------------- ---- ----- ------ -----
novel 1991 1-861001-57-6 Emma author 19.95
novel 1982 1-861001-45-3 Sense and Sensibility author 19.95
$doc.bookstore.book | Where-Object publicationdate -lt 1992 | Sort publicationdate | select title
仅对<title>字段进行排序和打印
title
-----
Sense and Sensibility
Emma
对数据进行切片和切块的方法还有很多,这完全取决于您想要做什么。
- I've created a bug report for the `Select-Xml` encoding problem: https://github.com/PowerShell/PowerShell/issues/14404
- @zett42 That's amazing! I've never tried it, but `Select-Xml` actually messes this up (I tried, my PS Version is 5.1.18362). This is an actual bug in PowerShell, and an embarrassing one, too.
- @zett42, doing what is undoubtedly the right and most robust thing here is indeed so cumbersome - and obscure - that people will keep taking the `[xml] (Get-Content -Raw ...)` shortcut, unless we provide a PowerShell-idiomatic alternative that is both robust and convenient: please see https://github.com/PowerShell/PowerShell/issues/14505
THE END
二维码