Search Unity

  1. Click here to see what's on sale for the "Best of Super Sale" on the Asset Store
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

RSS Reading into Unity Example

Discussion in 'Scripting' started by zumwalt, Sep 24, 2010.

  1. zumwalt

    zumwalt

    Joined:
    Apr 18, 2007
    Posts:
    2,207
    Oddly, I have had emails from people asking about XML and possibly reading RSS files, most of them use it for an interaction between the forums they have and in their game, so I figured that I would post an example here in the forums, might get around to putting this in the wiki later if I make it a lot more dynamic, this example package though, reads the Unity RSS feed and prints to the debug window the RSS data, this is just so you can get a feeling on how simple this really is to do.
     

    Attached Files:

  2. alcamedes

    alcamedes

    Joined:
    Mar 26, 2014
    Posts:
    5
    I think I love you. Can't believe this thread hasn't gotten more attention. Been trying to do this for two days to no avail. Saved me big time. Thanks!
     
  3. Raptcha911

    Raptcha911

    Joined:
    Sep 19, 2014
    Posts:
    17
    Thanks a lot!!...I was looking for something precisely like this..I had to tailor it to my needs, but the base code really helped me understand how RSS integration in unity works!!!
     
  4. OJ3D

    OJ3D

    Joined:
    Feb 13, 2014
    Posts:
    33
  5. kevinbunity

    kevinbunity

    Joined:
    Feb 16, 2016
    Posts:
    1
    Thanks! Very nice way to do this and easy to understand.
     
  6. Gokuofuin

    Gokuofuin

    Joined:
    Jun 30, 2015
    Posts:
    20
    Here is an improved version for redundancy of different RSS feeds containing different things, thanks Zumwalt for the boost.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using System.Xml;
    5.  
    6. public class rssreader
    7. {
    8.     XmlTextReader rssReader;
    9.     XmlDocument rssDoc;
    10.     XmlNode nodeRss;
    11.     XmlNode nodeChannel;
    12.     XmlNode nodeItem;
    13.     public channel rowNews;
    14.  
    15.     // this is the root channel information within the RSS
    16.     // I have supplied the fields here that are in the UNITY RSS feed
    17.     public struct channel
    18.     {
    19.         public string title;
    20.         public string link;
    21.         public string description;
    22.         public string docs;
    23.         public string managingEditor;
    24.         public string webMaster;
    25.         public string lastBuildDate;
    26.         // this is our collection of RSS items
    27.         public List<items> item;
    28.     }
    29.  
    30.     // again, all values here are the same as what exists in the UNITY RSS feed
    31.     public struct items
    32.     {
    33.         public string title;
    34.         public string category;
    35.         public string creator;
    36.         public string guid;
    37.         public string link;
    38.         public string pubDate;
    39.         public string description;
    40.     }  
    41.  
    42.     // our constructor takes the URL to the feed
    43.     public rssreader (string feedURL)
    44.     {
    45.         // setup the channel structure
    46.         rowNews = new channel ();
    47.         // make the list available to write to
    48.         rowNews.item = new List<items>();
    49.         rssReader = new XmlTextReader (feedURL);
    50.         rssDoc = new XmlDocument ();
    51.         rssDoc.Load (rssReader);
    52.         // Loop for the <rss> tag
    53.         if(rssDoc != null)
    54.         {
    55.             for (int i = 0; i < rssDoc.ChildNodes.Count; i++)
    56.             {
    57.                 // If it is the rss tag
    58.                 if (rssDoc.ChildNodes[i].Name == "rss") {
    59.                     // <rss> tag found
    60.                     nodeRss = rssDoc.ChildNodes[i];
    61.                 }
    62.             }
    63.         }
    64.          
    65.         // Loop for the <channel> tag
    66.         if(nodeRss != null)
    67.         {
    68.             for (int i = 0; i < nodeRss.ChildNodes.Count; i++)
    69.             {
    70.                 // If it is the channel tag
    71.                 if (nodeRss.ChildNodes[i].Name == "channel")
    72.                 {
    73.                     // <channel> tag found
    74.                     nodeChannel = nodeRss.ChildNodes[i];
    75.                 }
    76.             }
    77.         }
    78.  
    79.         // this is our channel header information
    80.         if(rowNews.title != null)
    81.         {
    82.             rowNews.title = nodeChannel["title"].InnerText;
    83.         }
    84.         if(rowNews.link != null)
    85.         {
    86.             rowNews.link = nodeChannel["link"].InnerText;
    87.         }
    88.         if(rowNews.description != null)
    89.         {
    90.             rowNews.description = nodeChannel["description"].InnerText;
    91.         }
    92.         if(rowNews.docs != null)
    93.         {
    94.             rowNews.docs = nodeChannel["docs"].InnerText;
    95.         }
    96.         if(rowNews.lastBuildDate != null)
    97.         {
    98.             rowNews.lastBuildDate = nodeChannel["lastBuildDate"].InnerText;
    99.         }
    100.         if(rowNews.managingEditor != null)
    101.         {
    102.             rowNews.managingEditor = nodeChannel["managingEditor"].InnerText;
    103.         }
    104.         if(rowNews.webMaster != null)
    105.         {
    106.             rowNews.webMaster = nodeChannel["webMaster"].InnerText;
    107.         }
    108.  
    109.         // here we have our feed items
    110.         if(nodeChannel != null)
    111.         {
    112.             for (int i = 0; i < nodeChannel.ChildNodes.Count; i++)
    113.             {
    114.                 if (nodeChannel.ChildNodes[i].Name == "item")
    115.                 {
    116.                     nodeItem = nodeChannel.ChildNodes[i];
    117.                     // create an empty item to fill
    118.                     items itm = new items();
    119.                     if(nodeItem.InnerXml.Contains("title"))
    120.                     {
    121.                         itm.title = nodeItem["title"].InnerText;
    122.                     }
    123.                     if(nodeItem.InnerXml.Contains("link"))
    124.                     {
    125.                         itm.link = nodeItem["link"].InnerText;
    126.                     }
    127.                     if(nodeItem.InnerXml.Contains("category"))
    128.                     {
    129.                         itm.category = nodeItem["category"].InnerText;
    130.                     }
    131.                     if(nodeItem.InnerXml.Contains("dc:creator"))
    132.                     {
    133.                         itm.creator = nodeItem["dc:creator"].InnerText;
    134.                     }
    135.                     if(nodeItem.InnerXml.Contains("guid"))
    136.                     {
    137.                         itm.guid = nodeItem["guid"].InnerText;
    138.                     }
    139.                     if(nodeItem.InnerXml.Contains("pubDate"))
    140.                     {
    141.                         itm.pubDate = nodeItem["pubDate"].InnerText;
    142.                     }
    143.                     if(nodeItem.InnerXml.Contains("description"))
    144.                     {
    145.                         itm.description = nodeItem["description"].InnerText;
    146.                     }
    147.                     // add the item to the channel items list
    148.                     rowNews.item.Add(itm);
    149.                 }
    150.             }
    151.         }
    152.  
    153.     }
    154. }
     
    Harrison_Perry and Kael-kael like this.
  7. Harrison_Perry

    Harrison_Perry

    Joined:
    Jan 16, 2017
    Posts:
    8
    Aye, still working noice in 2020 - cheers, dude!!
     
unityunity