xml 如何使用powershell从Web读取XML文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1819341/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to read XML file from web using powershell
提问by Sitaram Pamarthi
When I connect to my HP ILO using the URL, http://ilo-ip/xmldata?item=Allit is returning a XML file in below format.
当我使用 URL http://ilo-ip/xmldata?item=All连接到我的 HP ILO 时, 它返回以下格式的 XML 文件。
<?xml version="1.0" ?>
- <RIMP>
- <HSI>
<SPN>ProLiant DL385 G1</SPN>
<SBSN>ABCDEFG</SBSN>
<UUID>123456789</UUID>
</HSI>
- <MP>
<ST>1</ST>
<PN>Integrated Lights-Out (iLO)</PN>
<FWRI>1.82</FWRI>
<HWRI>ASIC: 2</HWRI>
<SN>ILOABCDEFG</SN>
<UUID>ILO1234545</UUID>
</MP>
</RIMP>
Is there any powershell command/script to read XML data from web URL?
是否有任何 powershell 命令/脚本可以从 Web URL 读取 XML 数据?
回答by
PowerShell uses the .Net framework XmlDocument so you could call the load method to load the xml into an XmlDocument object like this:
PowerShell 使用 .Net 框架 XmlDocument,因此您可以调用 load 方法将 xml 加载到 XmlDocument 对象中,如下所示:
#Option 1
$doc = New-Object System.Xml.XmlDocument
$doc.Load("http://ilo-ip/xmldata?item=All")
#Option 2
[xml]$doc = (New-Object System.Net.WebClient).DownloadString("http://ilo-ip/xmldata?item=All")
# Your XML will then be in the $doc and the names of the XML nodes can be used as
# properties to access the values they hold. This short example shows how you would
# access the value held in the SPN nodes from your sample XML
Write-Host $doc.RIMP.HSI.SPN
If your looking for more information on working with the XML once it has been loaded check out Dr. Tobias Weltner's eBookat PowerShell.com, chapter 14covers XML.
如果您在加载 XML 后寻找有关使用 XML 的更多信息,请查看PowerShell.com 上Tobias Weltner 博士的电子书,第 14 章介绍了 XML。
回答by Keith Hill
Sure. You can use the .NET WebClient object to download XML from the web and then use the [xml] type to interpret a string as XML like so:
当然。您可以使用 .NET WebClient 对象从 Web 下载 XML,然后使用 [xml] 类型将字符串解释为 XML,如下所示:
$url = "http://blogs.msdn.com/powershell/rss.xml"
[xml]$xml = (new-object System.Net.WebClient).DownloadString($url)
$xml.rss.channel | Foreach {$_.item} |
Where {[DateTime]$_.pubDate -gt (Get-Date).AddMonths(-1)} |
Format-Table Title

