Sunday 7 December 2008

Write-RSSStream.ps1

# Write-RSSStream.ps1
#
This sample shows how to use the XMLTextWriter
#
Based on Halr's content, but extended.
#
Thomas Lee - tfl@psp.co.uk

# Define output
$path = "c:\foo\file.xml"

# Create encoding, and create xml writer

$encoding = [System.Text.Encoding]::UTF8
$writer = New-Object System.Xml.XmlTextWriter( $Path, $encoding )
$writer.Formatting = [system.xml.formatting]::indented

# Write start of document
$writer.WriteStartDocument()

# Write Start of rss stream
$writer.WriteStartElement( "rss" )
$writer.WriteAttributeString( "version", "2.0" )

# write channel start info
$writer.WriteStartElement("channel")
$writer.WriteStartElement("title")
$writer.writestring("My Channel")
$writer.WriteEndElement() #end title
$Writer.WriteStartElement("Last Build Date")
$writer.WriteString((Get-Date).ToString())
$writer.WriteEndElement() #end last build date
$writer.WriteStartElement("subtitle")
$writer.WriteString("This Blog's subtitle")
$writer.WriteEndElement() #end subtitle

# Now here put an item in
$writer.WriteStartElement("item")
$writer.WriteStartElement("title")
$writer.WriteString("A cool RSS Feed Item")
$writer.WriteEndElement() #end title
$writer.WriteStartElement("link")
$writer.WriteString("http://pshscripts.blogspot.com")
$writer.WriteEndElement() #end link
$writer.WriteStartElement("description")
$writer.WriteString("This is the body of a blog post. It could go on, and on. ")
$writer.Writestring("Or not as the case may be!")
$writer.WriteEndElement() #end description
$writer.WriteStartElement("author")
$writer.WriteString("Thomas Lee - tfl@psp.co.uk")
$writer.WriteEndElement() #end author
$writer.WriteEndElement() #end item

# Write end of document information
$writer.WriteEndElement() # End Channel
$writer.WriteEndElement() #End RSS

# Make sure we close the file
$writer.close()

# Let's see what it has done
cat $path

This script produces the following output:

PS C:\foo> . 'C:\foo\write-rssstream.PS1'
<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>My Channel</title>
<Last Build Date>12/5/2008 8:50:26 AM</Last Build Date>
<subtitle>This Blog's subtitle</subtitle>
<item>
<title>A cool RSS Feed Item</title>
<link>http://pshscripts.blogspot.com</link>
<description>This is the body of a blog post. It could go on, and on.
Or not
as the case may be!</description>
<author>Thomas Lee - tfl@psp.co.uk</author>
</item>
</channel>
</rss>

2 comments:

ARUN FRUITY said...

At last someone had the answer for what I was looking :) thanks a lot

ARUN FRUITY said...

At last someone had the answer for what I was looking for :) thanks a Ton