如何使用 Select-Xml 和 Xpath 在 Powershell 中解析 XML? [英] How to parse XML in Powershell with Select-Xml and Xpath?

查看:33
本文介绍了如何使用 Select-Xml 和 Xpath 在 Powershell 中解析 XML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个例子的明显输出是日期和时间.

obvious output for this example would be the date and time.

在迭代 xml 文档的上下文中,该文档如何正确转换或 上的 Powershell dotnet 核心中转换xmlLinux?

In the context of iterating over an xml document, how is this document correctly cast or converted to xml in Powershell dotnet core on Linux?

PS /home/nicholas/flwor> 
PS /home/nicholas/flwor> Select-Xml -Xml (Get-Date | ConvertTo-Xml) -XPath "/Objects/Object"

Node   Path        Pattern
----   ----        -------
Object InputStream /Objects/Object

PS /home/nicholas/flwor> 
PS /home/nicholas/flwor> (Get-Date | ConvertTo-Xml).OuterXml                                
<?xml version="1.0" encoding="utf-8"?><Objects><Object Type="System.DateTime">12/10/2020 5:12:45 AM</Object></Objects>
PS /home/nicholas/flwor> 

更新了最简单的例子.

所以,这个例子是 xml 但是:它是如何用 xpath 解析或查询的?

So, this example is xml but: how is it parsed or queried with xpath?

IRC 中给出的替代解决方案:

alternate solution given in IRC:

PS /home/nicholas/flwor> 
PS /home/nicholas/flwor> $date.SelectSingleNode('/Objects/Object').'#text'
12/10/2020 5:07:04 AM
PS /home/nicholas/flwor> 

毫无疑问,使用 Select-XmlXpath 是可行的.

no doubt that's doable with Select-Xml and Xpath.

推荐答案

如果尴尬,这有效:

PS /home/nicholas/flwor> 
PS /home/nicholas/flwor> Select-Xml -Xml (Get-Date | ConvertTo-Xml) -XPath "/Objects/Object" |  Select-Object -ExpandProperty Node

Type            #text
----            -----
System.DateTime 12/10/2020 5:23:39 AM

PS /home/nicholas/flwor> 

帮助文件有一个类似的例子.

the help files had a similar example.

这篇关于如何使用 Select-Xml 和 Xpath 在 Powershell 中解析 XML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆