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

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

问题描述

此示例的明显输出将是日期和时间.

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

在遍历 xml 文档的上下文中,如何正确投射此文档或已转换 Powershell dotnet核心中的 xml Linux ?

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-Xml Xpath 一起使用.

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天全站免登陆