Thursday, April 3, 2014

How to read online xml using PowerShell?

This post demonstrate how you could read an xml file and manipulate using PowerShell.
# I have taken this site's sitemap as sample xml

$onlineXMLSitemap = "https://hemantrohtak.blogspot.com/sitemap.xml" ;
$doc = New-Object System.Xml.XmlDocument;
$doc.Load($onlineXMLSitemap);

# you could manipulate this xml just like an ordinary xml in object model
#below mentioned: Print a random url from my sitemap

$doc.urlset.url | Get-Random -count 1 | ForEach-Object {

Write-Host $_.loc ;

}

Sample Code :

# I have taken this site's sitemap as sample xml
$onlineXMLSitemap = "https://hemantrohtak.blogspot.com/sitemap.xml" ;
$doc = New-Object System.Xml.XmlDocument;
$doc.Load($onlineXMLSitemap);
# you could manipulate this xml just like an ordinary xml in object model

No comments:

Post a Comment