如何使用 ConvertTo-Xml 和 Select-Xml 加载或读取 XML 文件?

Nic*_*ers 1 xml powershell xpath xquery select-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> 
Run Code Online (Sandbox Code Playgroud)

但是,相反,读取文件?


如何加载/导入/读取/转换用于解析的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> 
Run Code Online (Sandbox Code Playgroud)

xmlREPL控制台本身中创建文件按预期工作:

如何使用 Select-Xml 和 Xpath 在 Powershell 中解析 XML?

Tom*_*lak 6

在 Powershell 中正确读取 XML 文档的工作方式如下:

$doc = New-Object xml
$doc.Load( (Convert-Path bookstore.xml) )
Run Code Online (Sandbox Code Playgroud)

XML 可以采用多种文件编码,使用XmlDocument.Load方法可确保在事先不了解编码的情况下正确读取文件。

不读取具有正确编码的文件将导致数据损坏或错误,除非在非常基本或非常幸运的情况下。

由于这个原因,经常看到的使用Get-Content和转换结果字符串[xml]的方法是处理 XML 的错误方法。所以不要那样做。

您可以使用 获得正确的结果Get-Content,但这需要

  1. 文件编码的先验知识(例如Get-Content bookstore.xml -Encoding UTF8
  2. 硬编码的文件编码到脚本(这意味着如果XML编码这将打破以往骤变)
  3. 将自己限制为支持的极少数文件编码Get-Content(XML 支持更多)

这意味着您将自己置于必须手动思考和解决 XML 专门设计为自动为您处理的问题的位置。

正确地做事Get-Content会带来很多不必要的额外工作和限制。当做正确的事情很容易时,做错的事情毫无意义。


示例,加载后$doc如上图所示。

$doc.bookstore.book
Run Code Online (Sandbox Code Playgroud)

打印<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
Run Code Online (Sandbox Code Playgroud)
$doc.bookstore.book | Format-Table
Run Code Online (Sandbox Code Playgroud)

打印与表格相同的东西

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
Run Code Online (Sandbox Code Playgroud)
$doc.bookstore.book | Where-Object publicationdate -lt 1992 | Format-Table
Run Code Online (Sandbox Code Playgroud)

过滤数据

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
Run Code Online (Sandbox Code Playgroud)
$doc.bookstore.book | Where-Object publicationdate -lt 1992 | Sort publicationdate | select title
Run Code Online (Sandbox Code Playgroud)

仅对<title>字段进行排序和打印

title                
-----                
Sense and Sensibility
Emma
Run Code Online (Sandbox Code Playgroud)

对数据进行切片和切块的方法还有很多,这完全取决于您想要做什么。

  • 我已经为“Select-Xml”编码问题创建了错误报告:https://github.com/PowerShell/PowerShell/issues/14404 (3认同)
  • @zett42 太棒了!我从来没有尝试过,但是 `Select-Xml` 实际上搞砸了(我尝试过,我的 PS 版本是 5.1.18362)。这是 PowerShell 中的一个实际错误,也是一个令人尴尬的错误。 (2认同)
  • @zett42,在这里做无疑是正确和最稳健的事情确实是如此麻烦和晦涩,以至于人们会继续采用“[xml](Get-Content -Raw ...)”快捷方式,除非我们提供 PowerShell -既强大又方便的惯用替代方案:请参阅https://github.com/PowerShell/PowerShell/issues/14505 (2认同)