首页 > 解决方案 > 如何使用 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> 

但是,相反,读取文件?


如何使用 using 加载/导入/读取/转换xml文件以进行ConvertTo-Xml解析?Select-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?

标签: xmlpowershellxpathxqueryselect-xml

解决方案


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

$doc = New-Object xml
$doc.Load( (Convert-Path bookstore.xml) )

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

打印<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

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


推荐阅读