Read an RSS feed from SSIS

Published Mar 25 2019 02:24 PM 797 Views
Not applicable
First published on MSDN on Feb 19, 2009

The 3.5 .NET framework introduced a new SyndicationFeed class which simplifies the process of reading (and creating) RSS feeds . To try it out, I threw together a simple SSIS package which reads from an RSS feed using a Script Component.

Things to note:

  • I have an Http Connection Manager, but I’m not actually using its connection object in my script. I use it just for its connection string (the URL to the RSS feed)
  • You need to add a reference to System.ServiceModel.Web assembly to access the SyndicationFeed class
  • The Script Task / Script Component will target the 2.0 .NET framework by default. To use the SyndicationFeed class, you’ll need to use the 3.5 .NET framework. You can change this setting by right clicking on the script component’s Project, and selecting properties…

The code for the script (notice I’m using C# which is only supported in SSIS 2008):

[Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute]
public class ScriptMain : UserComponent
{
private string url = string.Empty;
private SyndicationFeed feed = null;
private XmlReader reader = null;

public override void PreExecute()
{
base.PreExecute();

// Get the URL from the Http Connection Manager.
// Note, we're not actually using the connection manager's connection object,
// just it's URL setting. This is because using the .NET connection classes
// give us more flexibility.
reader = XmlReader.Create(Connections.HttpConnection.ConnectionString);
feed = SyndicationFeed.Load(reader);
}

public override void PostExecute()
{
base.PostExecute();

reader.Close();
}

public override void CreateNewOutputRows()
{
if (feed != null)
{
foreach (var item in feed.Items)
{
Output0Buffer.AddRow();

Output0Buffer.Title = item.Title.Text;
Output0Buffer.PublishDate = item.PublishDate;
Output0Buffer.LastUpdatedTime = item.LastUpdatedTime;
Output0Buffer.Id = item.Id;
Output0Buffer.Summary.AddBlobData(ConvertToBytes(item.Summary));

string authorName = string.Empty;
if (item.Authors.Count > 0)
{
// take the first author
authorName = item.Authors[0].Name;
}
Output0Buffer.Author = authorName;
}

Output0Buffer.SetEndOfRowset();
}
}

private byte[] ConvertToBytes(TextSyndicationContent content)
{
if (content != null && !string.IsNullOrEmpty(content.Text))
{
// convert the string buffer to UTF8 so we can store it in an NTEXT column
var encoding = new UTF8Encoding();
return encoding.GetBytes(content.Text);
}

return new byte[0];
}
}


The package is available on my SkyDrive share:


%3CLINGO-SUB%20id%3D%22lingo-sub-387567%22%20slang%3D%22en-US%22%3ERead%20an%20RSS%20feed%20from%20SSIS%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-387567%22%20slang%3D%22en-US%22%3E%0A%20%26lt%3Bmeta%20http-equiv%3D%22Content-Type%22%20content%3D%22text%2Fhtml%3B%20charset%3DUTF-8%22%20%2F%26gt%3B%3CSTRONG%3E%20First%20published%20on%20MSDN%20on%20Feb%2019%2C%202009%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%3CP%3EThe%203.5%20.NET%20framework%20introduced%20a%20new%20%3CA%20href%3D%22http%3A%2F%2Fmsdn.microsoft.com%2Fen-us%2Flibrary%2Fsystem.servicemodel.syndication.syndicationfeed.aspx%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3E%20SyndicationFeed%20%3C%2FA%3E%20class%20which%20simplifies%20the%20process%20of%20reading%20(and%20creating)%20%3CA%20href%3D%22http%3A%2F%2Fen.wikipedia.org%2Fwiki%2FRss_feed%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3E%20RSS%20feeds%20%3C%2FA%3E%20.%20To%20try%20it%20out%2C%20I%20threw%20together%20a%20simple%20SSIS%20package%20which%20reads%20from%20an%20RSS%20feed%20using%20a%20Script%20Component.%3C%2FP%3E%0A%20%20%3CP%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F99455i20446BFA095D8CC5%22%20%2F%3E%3C%2FP%3E%0A%20%20%3CP%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F99456i008069C3ACA91022%22%20%2F%3E%3C%2FP%3E%0A%20%20%3CP%3EThings%20to%20note%3A%3C%2FP%3E%0A%20%20%3CUL%3E%0A%20%20%20%3CLI%3EI%20have%20an%20Http%20Connection%20Manager%2C%20but%20I%E2%80%99m%20not%20actually%20using%20its%20connection%20object%20in%20my%20script.%20I%20use%20it%20just%20for%20its%20connection%20string%20(the%20URL%20to%20the%20RSS%20feed)%3C%2FLI%3E%0A%20%20%20%3CLI%3EYou%20need%20to%20add%20a%20reference%20to%20System.ServiceModel.Web%20assembly%20to%20access%20the%20SyndicationFeed%20class%3C%2FLI%3E%0A%20%20%20%3CLI%3EThe%20Script%20Task%20%2F%20Script%20Component%20will%20target%20the%202.0%20.NET%20framework%20by%20default.%20To%20use%20the%20SyndicationFeed%20class%2C%20you%E2%80%99ll%20need%20to%20use%20the%203.5%20.NET%20framework.%20You%20can%20change%20this%20setting%20by%20right%20clicking%20on%20the%20script%20component%E2%80%99s%20Project%2C%20and%20selecting%20properties%E2%80%A6%3C%2FLI%3E%0A%20%20%3C%2FUL%3E%0A%20%20%3CP%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F99457iF1D43629E12881C8%22%20%2F%3E%3C%2FP%3E%0A%20%20%3CP%3EThe%20code%20for%20the%20script%20(notice%20I%E2%80%99m%20using%20C%23%20which%20is%20only%20supported%20in%20SSIS%202008)%3A%3C%2FP%3E%5BMicrosoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute%5D%20%3CBR%20%2F%3E%20public%20class%20ScriptMain%20%3A%20UserComponent%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20private%20string%20url%20%3D%20string.Empty%3B%20%3CBR%20%2F%3E%20private%20SyndicationFeed%20feed%20%3D%20null%3B%20%3CBR%20%2F%3E%20private%20XmlReader%20reader%20%3D%20null%3B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20public%20override%20void%20PreExecute()%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20base.PreExecute()%3B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%2F%2F%20Get%20the%20URL%20from%20the%20Http%20Connection%20Manager.%20%3CBR%20%2F%3E%20%2F%2F%20Note%2C%20we're%20not%20actually%20using%20the%20connection%20manager's%20connection%20object%2C%20%3CBR%20%2F%3E%20%2F%2F%20just%20it's%20URL%20setting.%20This%20is%20because%20using%20the%20.NET%20connection%20classes%20%3CBR%20%2F%3E%20%2F%2F%20give%20us%20more%20flexibility.%20%3CBR%20%2F%3E%20reader%20%3D%20XmlReader.Create(Connections.HttpConnection.ConnectionString)%3B%20%3CBR%20%2F%3E%20feed%20%3D%20SyndicationFeed.Load(reader)%3B%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20public%20override%20void%20PostExecute()%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20base.PostExecute()%3B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20reader.Close()%3B%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20public%20override%20void%20CreateNewOutputRows()%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20if%20(feed%20!%3D%20null)%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20foreach%20(var%20item%20in%20feed.Items)%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20Output0Buffer.AddRow()%3B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Output0Buffer.Title%20%3D%20item.Title.Text%3B%20%3CBR%20%2F%3E%20Output0Buffer.PublishDate%20%3D%20item.PublishDate%3B%20%3CBR%20%2F%3E%20Output0Buffer.LastUpdatedTime%20%3D%20item.LastUpdatedTime%3B%20%3CBR%20%2F%3E%20Output0Buffer.Id%20%3D%20item.Id%3B%20%3CBR%20%2F%3E%20Output0Buffer.Summary.AddBlobData(ConvertToBytes(item.Summary))%3B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20string%20authorName%20%3D%20string.Empty%3B%20%3CBR%20%2F%3E%20if%20(item.Authors.Count%20%26gt%3B%200)%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20%2F%2F%20take%20the%20first%20author%20%3CBR%20%2F%3E%20authorName%20%3D%20item.Authors%5B0%5D.Name%3B%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20Output0Buffer.Author%20%3D%20authorName%3B%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Output0Buffer.SetEndOfRowset()%3B%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20private%20byte%5B%5D%20ConvertToBytes(TextSyndicationContent%20content)%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20if%20(content%20!%3D%20null%20%26amp%3B%26amp%3B%20!string.IsNullOrEmpty(content.Text))%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20%2F%2F%20convert%20the%20string%20buffer%20to%20UTF8%20so%20we%20can%20store%20it%20in%20an%20NTEXT%20column%20%3CBR%20%2F%3E%20var%20encoding%20%3D%20new%20UTF8Encoding()%3B%20%3CBR%20%2F%3E%20return%20encoding.GetBytes(content.Text)%3B%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20return%20new%20byte%5B0%5D%3B%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20%3CA%20href%3D%22http%3A%2F%2F11011.net%2Fsoftware%2Fvspaste%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3E%20%3C%2FA%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CP%3EThe%20package%20is%20available%20on%20my%20SkyDrive%20share%3A%3C%2FP%3E%3CBR%20%2F%3E%20%3CIFRAME%20frameborder%3D%220%22%20src%3D%22http%3A%2F%2Fcid-2aeb3aa8bb4bd9fd.skydrive.live.com%2Fembedrowdetail.aspx%2FPublic%2FRssFeed.dtsx%22%3E%20%3C%2FIFRAME%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-387567%22%20slang%3D%22en-US%22%3EFirst%20published%20on%20MSDN%20on%20Feb%2019%2C%202009%20The%203.%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-387567%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3Escript%20task%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Version history
Last update:
‎Mar 25 2019 02:24 PM
Updated by: