2015-03-08 99 views
0

阅读元素我有这样的XML库门PowerShell的 - 我怎么从一个XML文件

<?xml version="1.0" encoding="utf-8"?> 
<rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"> 
<channel> 
<title>ps read XML File</title> 
<description>Some Text</description> 
<item> 
<title>Element One</title> 
<description>Some Text to Element ONE</description> 
<guid>http://dlbmodigital.microsoft.com/webcasts/wmv/23976_Dnl_L.wmv</guid> 
</item> 
<item> 
<title>Element Two</title> 
<description>Some Text to Element TWO</description> 
<guid>http://dlbmodigital.microsoft.com/webcasts/wmv/23977_Dnl_L.wmv</guid> 
</item> 
<item> 
<title>Element Three</title> 
<description>Some Text to Element Three</description> 
<guid>http://dlbmodigital.microsoft.com/webcasts/wmv/23978_Dnl_L.wmv</guid> 
</item> 
</channel> 
</rss> 

我想赶从元素“GUID”的值。但我不知道如何。

有了这个代码

"[xml]$xmldata = Get-Content -path C:\tmp\ps\test-ps.xml 
$xmldata.rss.channel.item" 

我看到了三个 “项目” 的元素。但我不知道如何从元素“guid”中捕捉到值(超链接)。

回答

0

如果你希望所有的GUID元素,那么你可以使用XPath这样的:

$xmldata.SelectNodes('//guid') | Select-Object -ExpandProperty "#text" 

http://dlbmodigital.microsoft.com/webcasts/wmv/23976_Dnl_L.wmv 
http://dlbmodigital.microsoft.com/webcasts/wmv/23977_Dnl_L.wmv 
http://dlbmodigital.microsoft.com/webcasts/wmv/23978_Dnl_L.wmv 
+0

感谢您使用xpath提示。我必须阅读它是如何工作的。 我的目标是找到所有超链接(Element guid)并下载它。 用VB我统计guid元素的数量,然后用For循环下载(int_i到计数)。 随着Powershell我不得不学习,如何访问guid元素的方式。 – ral9004 2015-03-11 18:03:08

+0

然后这正是你想要的(除了下载它)。添加'| Foreach-object {#你的下载代码}',你就完成了。但是除非你有条件去过滤一些guid元素,然而@ mtman的点源方法更清晰。我添加了这个给你一个与所有PowerShell版本兼容的不同解决方案。:-) – 2015-03-11 20:20:02

0

你可以通过简单地引用他们遍历所有的孩子:

$xmldata.rss.channel.item.guid 

但是,你不为他们获得自动完成。 (这只是巧合发生在“项目”,因为这是也是一个函数)。

+1

仅供参考,仅适用于V3和因为V3添加了成员​​枚举功能,允许您在集合中的对象上“点”一个属性,即item是一个集合。在V2中,你必须这样做:'$ xmldata.rss.channel.item | Foreach {$ _。guid}'。 – 2015-03-08 20:59:33

+0

完美!有用。谢谢! – ral9004 2015-03-09 08:25:18

相关问题