如何使用 ConvertTo-Xml 和 Select-Xml 加载或读取 XML 文件? [英] How to load or read an XML file using ConvertTo-Xml and Select-Xml?
问题描述
我怎样才能完成这样的事情:
How can I accomplish something like this:
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>
但是,相反,读取文件?
but, instead, reading in a file?
如何使用 ConvertTo-Xml
加载/导入/读取/转换 xml
文件,以便使用 Select-Xml
进行解析>Xpath?
how do I load/import/read/convert an xml
file using ConvertTo-Xml
for parsing with Select-Xml
using Xpath
?
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>
在 REPL
控制台中创建 xml
文件本身按预期工作:
Creating the xml
file within the REPL
console itself works as expected:
如何使用 Select-Xml 和 Xpath 在 Powershell 中解析 XML?
推荐答案
正确在 Powershell 中读取 XML 文档的工作方式如下:
Properly reading an XML document in Powershell works like this:
$doc = New-Object xml
$doc.Load( (Convert-Path bookstore.xml) )
XML 可以有多种文件编码,并且使用 XmlDocument.Load
方法 确保在不了解编码的情况下正确读取文件.
XML can come in numerous file encodings, and using the XmlDocument.Load
method makes sure the file is read properly without prior knowledge of the encoding.
不读取具有正确编码的文件将导致数据损坏或错误,除非在非常基本或非常幸运的情况下.
Not reading a file with the correct encoding will result in mangled data or errors except in very basic or very lucky cases.
使用 Get-Content
并将结果字符串转换为 [xml]
的常见方法正是出于这个原因处理 XML 的错误方法.所以不要那样做.
The often-seen method of using Get-Content
and casting the resulting string to [xml]
is the wrong way of dealing with XML for this very reason. So don't do that.
您可以使用 Get-Content
获得正确的结果,但这需要
You can get a correct result with Get-Content
, but that requires
- 事先了解文件编码(例如
Get-Content bookstore.xml -Encoding UTF8
) - 将文件编码硬编码到您的脚本中(这意味着如果 XML 编码 永远 意外更改,它将中断)
- 仅限于
Get-Content
支持的极少数文件编码(XML 支持更多)
- Prior knowledge of the file encoding (e.g.
Get-Content bookstore.xml -Encoding UTF8
) - Hard-coding the file encoding into your script (meaning it will break if the XML encoding ever changes unexpectedly)
- Limiting yourself to the very few file encodings that
Get-Content
supports (XML supports more)
这意味着您将自己置于必须手动思考和解决 XML 专门为自动处理的问题而设计的位置.
It means you put yourself in a position where you have to manually think about and solve a problem that XML has been specifically designed to automatically handle for you.
使用 Get-Content
正确地做事是很多不必要的额外工作和限制.做正确的事很容易,做错的事毫无意义.
Doing things correctly with Get-Content
is a lot of unnecessary extra work and limitations. And doing things incorrectly is pointless when doing it right is so easy.
示例,加载 $doc
后,如上所示.
Examples, after loading $doc
like shown above.
$doc.bookstore.book
打印
元素及其属性的列表
prints a list of <book>
elements and their properties
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
仅对
字段进行排序和打印
sorts and prints only the <title>
field
title
-----
Sense and Sensibility
Emma
对数据进行切片和切块的方法还有很多,这完全取决于您想要做什么.
There are many more ways of slicing and dicing the data, it all depends on what you want to do.
这篇关于如何使用 ConvertTo-Xml 和 Select-Xml 加载或读取 XML 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!