SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 28
  1. #1
    SitePoint Guru Raskolnikov's Avatar
    Join Date
    Jul 2003
    Location
    USA
    Posts
    606
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Connecting to RSS feed with C#.net

    Anybody got any quick tips.

    I have an RSS feed that i would like to include stories on my site from. i don't even know where to begin with this one.

    Ras

  2. #2
    ALT.NET - because we need it silver trophybronze trophy dhtmlgod's Avatar
    Join Date
    Jul 2001
    Location
    Scotland
    Posts
    4,836
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  3. #3
    SitePoint Guru Raskolnikov's Avatar
    Join Date
    Jul 2003
    Location
    USA
    Posts
    606
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks.....strangely enough i already did that exact same search before i posted this thread. But i appreciate the note.

    I am just confused. With php, all i needed was an aggrigator, then i could create a while loop and just loop through the stories. With .net it seems that you have to implement a separate .dll file, then somhow work it into your web.config file.

    Now does anyone have any constructive thoughts. ie. how is this Bandit thing, are there better aggrigators than others etc.

    Ras

  4. #4
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,653
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    RSS Bandit is a desktop aggregator, not a library for aggregating stuff into your application.

    Exactly how to handle this depends on how you are structuring the app. I usually prefer to pull the RSS feeds, parse them (usually using a combo of XSLT + xml deserialization) and put them in my database. But that might be overkill here.

    Anyhow, in 2.0, you can just use the XmlDatasource and drag and drop it in, or you could use RSS.NET if you needed something more robust.

  5. #5
    SitePoint Guru Raskolnikov's Avatar
    Join Date
    Jul 2003
    Location
    USA
    Posts
    606
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Anyhow, in 2.0, you can just use the XmlDatasource
    you are refering to .net 2.0, not RSS 2.0 correct?

  6. #6
    ALT.NET - because we need it silver trophybronze trophy dhtmlgod's Avatar
    Join Date
    Jul 2001
    Location
    Scotland
    Posts
    4,836
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah

  7. #7
    SitePoint Guru Raskolnikov's Avatar
    Join Date
    Jul 2003
    Location
    USA
    Posts
    606
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I found this little tidbit.

    Code:
    XmlDocument xmlDoc = new XmlDocument();
    
    xmlDoc.Load("http://www.pgatour.com/partners/feeds/rss/home_news");
    				
    XmlNodeList nodeList = xmlDoc.SelectNodes("//item");
    			
    Response.Write(nodeList.Count.ToString());

    However, this is only telling me how many items are in the xml document. I am not real sure how i can display the individual item data. Would i need to create a Repeater?

  8. #8
    SitePoint Evangelist Muller2's Avatar
    Join Date
    Feb 2005
    Location
    Oxford, UK
    Posts
    522
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the code you have only counts the items within the xml feed, it doesn't actually do anything else. If you want to do something with the data (which I believe you do) then this might be a good starting point.

    http://aspnet.4guysfromrolla.com/articles/031903-1.aspx

    If you need to convert the vb.net to c# then just use an online converter. If you are looking at doing something else like store the content into a sql db then let me know.

    Also one little pointer - using Response.write(); isn't the best solution. You should apply the results to a label or use a literial or something.

  9. #9
    SitePoint Enthusiast
    Join Date
    Mar 2000
    Posts
    93
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    RSS Tooolkit - library for both consuming and exposing rss

    http://blogs.msdn.com/dmitryr/archiv...21/536552.aspx
    Stefan
    http://www.aspcode.net - articles on ASP/ASP.NET
    http://www.findfreefonts.net - jquery/ajax experiment site

  10. #10
    SitePoint Addict Chillijam's Avatar
    Join Date
    Nov 2003
    Location
    England
    Posts
    293
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Extending the code you posted earlier....

    Code:
    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.Load("http://www.pgatour.com/partners/feeds/rss/home_news");
    XmlNodeList nodeList = xmlDoc.SelectNodes("//item");
    foreach(XmlNode item in nodelist)
    {
      string url = item.SelectSingleNode("link");
      string title = item.SelectSingleNode("title");
      Response.Write("<a href=\""+url+"\">"+title+"</a><br />";
    }
    Of course, this code is intested for typos, and is only intended as a pointer to extend what you'd already found. Better solutions may exist.
    Your mind is like a parachute. It works best when open.
    (HH The Dalai Lama)

  11. #11
    SitePoint Guru Raskolnikov's Avatar
    Join Date
    Jul 2003
    Location
    USA
    Posts
    606
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Chilli,

    i am testing this with your method. It is looping corectly (7 items 7 loops), but the output data is being displayed as "System.Xml.XmlElement"

    all 7 of them. the url is also "System.Xml.XmlElement".

    what would cause this. I looked at the xml page, and each item definitely had a "title" part and a "url" part.

    I do not understand why it would return "System.Xml.XmlElement" instead of the actual data.

    any ideas?

    Ras

  12. #12
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,653
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    You need to select the title as the node then get the Text property, IIRC.

  13. #13
    SitePoint Guru Raskolnikov's Avatar
    Join Date
    Jul 2003
    Location
    USA
    Posts
    606
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, i decided i would try one of the third party readers listed above. I chose RSSToolkit. It seems to be just what i am looking for. however i do not think it is compatible with .net 1.1, but i need help confirming this.

    WHen i added these lines to my web.config in the compilation area:

    Code:
    <assemblies>
                    <add assembly="RssToolkit,Version=1.0.0.0,Culture=neutral,PublicKeyToken=d9202816019f11ea"/>
                    <add assembly="System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
                    <add assembly="System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
                </assemblies>
    
                <buildProviders>
                    <add extension=".rss" type="RssToolkit.RssBuildProvider,RssToolkit,Version=1.0.0.0,Culture=neutral,PublicKeyToken=d9202816019f11ea"/>
                    <add extension=".rssdl" type="RssToolkit.RssdlBuildProvider,RssToolkit,Version=1.0.0.0,Culture=neutral,PublicKeyToken=d9202816019f11ea"/>
                </buildProviders>
    I got an error stating "Compilation Error", and pointed at the third "add Assembly" line.

    I think this has to do with the fact that i am running .net 1.1, is this correct or should i be looking somewhere else for a mistake?

    Ras

  14. #14
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,653
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    It definitely is a 2.0 toolkit, so it will not work for you.

    I would really consider using the Xml control and Xslt here rather than getting into complex Xml parsing.

  15. #15
    SitePoint Guru Raskolnikov's Avatar
    Join Date
    Jul 2003
    Location
    USA
    Posts
    606
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    THis xml control is not all that rough. however i do have a question.

    i am using this to call the control:

    Code:
    <asp:Xml ID="xmlDisplay" Runat="server" DocumentSource="http://www.pgatour.com/partners/feeds/rss/home_news" TransformSource="pgatour_news.xslt"></asp:Xml>
    and i get an error saying it is looking for a direct path not a virtual path for the xml document.

    is there and alternative to DocumentSource? if not would i need to somehow save the xml file to my server first?

    thanks
    Ras

  16. #16
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,653
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    You will want to cache the file locally in some way, as making remote requests is very, very slow.

    Probably the best way to do this is to use ASP.NET's cache object as the backing store for your Xml, then set the Document property of the Xml control from codebehind to this item. I don't have any code handy, but it should be easy enough to figure out how to code 'er up.

  17. #17
    SitePoint Guru Raskolnikov's Avatar
    Join Date
    Jul 2003
    Location
    USA
    Posts
    606
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    so after diving into the Cache method, i came up with a couple lines of code that i thought might get the job done. However i believe that i am not quite doing all the steps required for this to work.

    Code:
    Cache.Insert("pga", "http://www.pgatour.com/partners/feeds/rss/home_news");
    xmlDisplay.DocumentSource=Cache.Get("pga").ToString();
    this is caching the url as a string and not the data in the xml page . SO i think i have to somhow take the .xml document and convert it into a dataset. does this sound correct?

    Ras

    EDIT: so i need to add a cache dependency ie:

    Code:
    CacheDependency dep = new CacheDependency("http://www.pgatour.com/partners/feeds/rss/home_news");
    Cache.Insert("DSN", pgaNews, dep);
    xmlDisplay.DocumentSource=Cache.Get("pga").ToString();
    however this is still not getting it done. the dependancy looks for a file path not a url. secondly i really don't know what the second argument in Cache.Insert "object" (in this case "pgaNews")needs to be.

  18. #18
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,653
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    The object needs to be the XmlDocument you get using the XmlDocument.Load function.

    You can then assign this back to the Xml control for it to render.

  19. #19
    SitePoint Guru Raskolnikov's Avatar
    Join Date
    Jul 2003
    Location
    USA
    Posts
    606
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    XmlDocument pgaNews = new XmlDocument();
    pgaNews.Load("http://www.pgatour.com/partners/feeds/rss/home_news");
    Cache.Insert("pga", pgaNews);
    xmlDisplay.Document=(XmlDocument)Cache.Get("pga");
    alright so using this returns no error, It does initiate the xsl file (the header row is displayed) but no data is in the table from the .xml file.

    i tried these:

    xmlDisplay.DocumentSource=Cache.Get("pga"); --- and it returned an error
    xmlDisplay.DocumentContent=Cache.Get("pga"); ---- and it returned an error

  20. #20
    SitePoint Guru Raskolnikov's Avatar
    Join Date
    Jul 2003
    Location
    USA
    Posts
    606
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i got to thinking. this may have to do with the xslt file.

    Code:
    <xsl:for-each select="channel/item">
    			<tr>
    
    				<td>
    				<xsl:value-of select="title"/>
    				</td>
    
    				<td>
    				<xsl:value-of select="link"/>
    				</td>
    				
    			</tr>
    			</xsl:for-each>
    that is the loop i have, and then the xml structure is located here:

    http://www.pgatour.com/partners/feeds/rss/home_news

    am i using the corect variables? i am wandering if maybe i am wrong at:

    select="channel/item"

  21. #21
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,653
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Not certain what you are using for XSLT. See below for my basic "transform RSS 2.0" xsl sheet:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    	<xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/>
    	<xsl:template match="*">
    		<html>
    			<head>
    				<title>
    					<xsl:value-of select="//channel/title" />
    				</title>
    			</head>
    			<body>
    					<h1>
    						<xsl:value-of select="//channel/title" />
    					</h1>
    					<p>How RSS Will Save the World</p>
    					<xsl:for-each select="//channel/item">
    							<div style="border: solid 1px black; padding: 5px; margin: 10px;">
    							<h2><a href="{link}"><xsl:value-of select="title" /></a></h2>
    							<strong><xsl:value-of select="pubDate" /></strong><br />
    							<xsl:value-of select="description" disable-output-escaping="yes" />							
    							</div>
    					</xsl:for-each>
    			</body>
    		</html>
    	</xsl:template>
    </xsl:stylesheet>
    Other protip: get a good XSL editor--CookTop is adequate for free--and test your XSL on that before getting into the .NET end. Always helps when you can be certain the XSL works.

  22. #22
    SitePoint Guru Raskolnikov's Avatar
    Join Date
    Jul 2003
    Location
    USA
    Posts
    606
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hey, that must have been my problem. works now! I think i might put together some sort of short "How To" guide to get this to work once i get it all solidified. I am sure there are other "New to .net" folks out there that would find this very helpful.

    Question about Cacheing. Having it set up like i do looks to me like it will cache the xml page everytime it loads is there a way to have the cache run only when content on the .xml page changes? i thought i read something somewhere that controled that.....maybe in a cache dependancy or something to that effect.

    Ras

  23. #23
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,653
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Cache dependencies for external items are tricky. For stuff like RSS, I would just give it an appropriate lifetime, say, 30 minutes, and run it like that.

    To check if it is expired, just see if the cached item is null.

  24. #24
    SitePoint Guru Raskolnikov's Avatar
    Join Date
    Jul 2003
    Location
    USA
    Posts
    606
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    xsl doesn't allow you to use the date format method within a for-each loop. How would you go about formating the date for each Item?

    Ras

  25. #25
    SitePoint Guru Raskolnikov's Avatar
    Join Date
    Jul 2003
    Location
    USA
    Posts
    606
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is what i found:

    http://www.w3.org/TR/xslt

    good website explaining bits and pieces of xslt code.

    Used the xsl:when/otherwise method to differentiate between the first entry and the rest of the entries, allowing me to format the newest entry different from the rest. secondly i was able to limit the neber of entries i displayed using this function.

    xslt is definitely a pretty cool way to display XML data within .net

    Ras


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •