private static RssFeed read(Stream stream) { RssFeed feed = new RssFeed(); RssElement element = null; if (stream != null) { RssReader reader = null; try { reader = new RssReader(stream); do { element = reader.Read(); if (element is RssChannel) { feed.Channels.Add((RssChannel)element); } }while (element != null); feed.rssVersion = reader.Version; } finally { feed.exceptions = reader.Exceptions; reader.Close(); } } else { throw new ApplicationException("Not a valid Url"); } return(feed); }
private static RssFeed read(string url, HttpWebRequest request, RssFeed oldFeed) { // ***** Marked for substantial improvement RssFeed feed = new RssFeed(); RssElement element = null; Stream stream = null; Uri uri = new Uri(url); feed.url = url; switch (uri.Scheme) { case "file": feed.lastModified = File.GetLastWriteTime(url); if ((oldFeed != null) && (feed.LastModified == oldFeed.LastModified)) { oldFeed.cached = true; return(oldFeed); } stream = new FileStream(url, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); break; case "https": goto case "http"; case "http": if (request == null) { request = (HttpWebRequest)WebRequest.Create(uri); } if (oldFeed != null) { request.IfModifiedSince = oldFeed.LastModified; request.Headers.Add("If-None-Match", oldFeed.ETag); } try { HttpWebResponse response = (HttpWebResponse)request.GetResponse(); feed.lastModified = response.LastModified; feed.etag = response.Headers["ETag"]; try { if (response.ContentEncoding != "") { feed.encoding = Encoding.GetEncoding(response.ContentEncoding); } } catch {} stream = response.GetResponseStream(); } catch (WebException we) { if (oldFeed != null) { oldFeed.cached = true; return(oldFeed); } else { throw we; // bad } } break; } if (stream != null) { RssReader reader = null; try { reader = new RssReader(stream); do { element = reader.Read(); if (element is RssChannel) { feed.Channels.Add((RssChannel)element); } }while (element != null); feed.rssVersion = reader.Version; } finally { feed.exceptions = reader.Exceptions; reader.Close(); } } else { throw new ApplicationException("Not a valid Url"); } return(feed); }
/// <summary>Reads the next RssElement from the stream.</summary> /// <returns>An RSS Element</returns> /// <exception cref="InvalidOperationException">RssReader has been closed, and can not be read.</exception> /// <exception cref="System.IO.FileNotFoundException">RSS file not found.</exception> /// <exception cref="System.Xml.XmlException">Invalid XML syntax in RSS file.</exception> /// <exception cref="System.IO.EndOfStreamException">Unable to read an RssElement. Reached the end of the stream.</exception> public RssElement Read() { bool readData = false; bool pushElement = true; RssElement rssElement = null; int lineNumber = -1; int linePosition = -1; if (reader == null) { throw new InvalidOperationException("RssReader has been closed, and can not be read."); } do { pushElement = true; try { readData = reader.Read(); } catch (System.IO.EndOfStreamException e) { throw new System.IO.EndOfStreamException("Unable to read an RssElement. Reached the end of the stream.", e); } catch (System.Xml.XmlException e) { if (lineNumber != -1 || linePosition != -1) { if (reader.LineNumber == lineNumber && reader.LinePosition == linePosition) { throw exceptions.LastException; } } lineNumber = reader.LineNumber; linePosition = reader.LinePosition; exceptions.Add(e); // just add to list of exceptions and continue :) } if (readData) { string readerName = reader.Name.ToLower(); switch (reader.NodeType) { case XmlNodeType.Element: { if (reader.IsEmptyElement) { break; } elementText = new StringBuilder(); switch (readerName) { case "item": // is this the end of the channel element? (absence of </channel> before <item>) if (!wroteChannel) { wroteChannel = true; rssElement = channel; // return RssChannel readData = false; } item = new RssItem(); // create new RssItem channel.Items.Add(item); break; case "source": source = new RssSource(); item.Source = source; for (int i = 0; i < reader.AttributeCount; i++) { reader.MoveToAttribute(i); switch (reader.Name.ToLower()) { case "url": try { source.Url = new Uri(reader.Value); } catch (Exception e) { exceptions.Add(e); } break; } } break; case "enclosure": enclosure = new RssEnclosure(); item.Enclosure = enclosure; for (int i = 0; i < reader.AttributeCount; i++) { reader.MoveToAttribute(i); switch (reader.Name.ToLower()) { case "url": try { enclosure.Url = new Uri(reader.Value); } catch (Exception e) { exceptions.Add(e); } break; case "length": try { enclosure.Length = int.Parse(reader.Value); } catch (Exception e) { exceptions.Add(e); } break; case "type": enclosure.Type = reader.Value; break; } } break; case "guid": guid = new RssGuid(); item.Guid = guid; for (int i = 0; i < reader.AttributeCount; i++) { reader.MoveToAttribute(i); switch (reader.Name.ToLower()) { case "ispermalink": try { guid.PermaLink = bool.Parse(reader.Value); } catch (Exception e) { exceptions.Add(e); } break; } } break; case "category": category = new RssCategory(); if ((string)xmlNodeStack.Peek() == "channel") { channel.Categories.Add(category); } else { item.Categories.Add(category); } for (int i = 0; i < reader.AttributeCount; i++) { reader.MoveToAttribute(i); switch (reader.Name.ToLower()) { case "url": goto case "domain"; case "domain": category.Domain = reader.Value; break; } } break; case "channel": channel = new RssChannel(RssDefault.String, RssDefault.String, RssDefault.Uri); textInput = null; image = null; cloud = null; source = null; enclosure = null; category = null; item = null; break; case "image": image = new RssImage(); channel.Image = image; break; case "textinput": textInput = new RssTextInput(); channel.TextInput = textInput; break; case "cloud": pushElement = false; cloud = new RssCloud(); channel.Cloud = cloud; for (int i = 0; i < reader.AttributeCount; i++) { reader.MoveToAttribute(i); switch (reader.Name.ToLower()) { case "domain": cloud.Domain = reader.Value; break; case "port": try { cloud.Port = ushort.Parse(reader.Value); } catch (Exception e) { exceptions.Add(e); } break; case "path": cloud.Path = reader.Value; break; case "registerprocedure": cloud.RegisterProcedure = reader.Value; break; case "protocol": switch (reader.Value.ToLower()) { case "xml-rpc": cloud.Protocol = RssCloudProtocol.XmlRpc; break; case "soap": cloud.Protocol = RssCloudProtocol.Soap; break; case "http-post": cloud.Protocol = RssCloudProtocol.HttpPost; break; default: cloud.Protocol = RssCloudProtocol.Empty; break; } break; } } break; case "rss": for (int i = 0; i < reader.AttributeCount; i++) { reader.MoveToAttribute(i); if (reader.Name.ToLower() == "version") { switch (reader.Value) { case "0.91": rssVersion = RssVersion.RSS091; break; case "0.92": rssVersion = RssVersion.RSS092; break; case "2.0": rssVersion = RssVersion.RSS20; break; default: rssVersion = RssVersion.NotSupported; break; } } } break; case "rdf": for (int i = 0; i < reader.AttributeCount; i++) { reader.MoveToAttribute(i); if (reader.Name.ToLower() == "version") { switch (reader.Value) { case "0.90": rssVersion = RssVersion.RSS090; break; case "1.0": rssVersion = RssVersion.RSS10; break; default: rssVersion = RssVersion.NotSupported; break; } } } break; } if (pushElement) { xmlNodeStack.Push(readerName); } break; } case XmlNodeType.EndElement: { if (xmlNodeStack.Count == 1) { break; } string childElementName = (string)xmlNodeStack.Pop(); string parentElementName = (string)xmlNodeStack.Peek(); switch (childElementName) // current element { // item classes case "item": rssElement = item; readData = false; break; case "source": source.Name = elementText.ToString(); rssElement = source; readData = false; break; case "enclosure": rssElement = enclosure; readData = false; break; case "guid": guid.Name = elementText.ToString(); rssElement = guid; readData = false; break; case "category": // parent is either item or channel category.Name = elementText.ToString(); rssElement = category; readData = false; break; // channel classes case "channel": if (wroteChannel) { wroteChannel = false; } else { wroteChannel = true; rssElement = channel; readData = false; } break; case "textinput": rssElement = textInput; readData = false; break; case "image": rssElement = image; readData = false; break; case "cloud": rssElement = cloud; readData = false; break; } switch (parentElementName) // parent element { case "item": switch (childElementName) { case "title": item.Title = elementText.ToString(); break; case "link": item.Link = new Uri(elementText.ToString()); break; case "description": item.Description = elementText.ToString(); break; case "author": item.Author = elementText.ToString(); break; case "comments": item.Comments = elementText.ToString(); break; case "pubdate": try { item.PubDate_GMT = DateTime.Parse(elementText.ToString()); } catch (Exception e) { try { string tmp = elementText.ToString(); tmp = tmp.Substring(0, tmp.Length - 5); tmp += "GMT"; item.PubDate_GMT = DateTime.Parse(tmp); } catch { exceptions.Add(e); } } break; } break; case "channel": switch (childElementName) { case "title": channel.Title = elementText.ToString(); break; case "link": try { channel.Link = new Uri(elementText.ToString()); } catch (Exception e) { exceptions.Add(e); } break; case "description": channel.Description = elementText.ToString(); break; case "language": channel.Language = elementText.ToString(); break; case "copyright": channel.Copyright = elementText.ToString(); break; case "managingeditor": channel.ManagingEditor = elementText.ToString(); break; case "webmaster": channel.WebMaster = elementText.ToString(); break; case "rating": channel.Rating = elementText.ToString(); break; case "pubdate": try { channel.PubDate = DateTime.Parse(elementText.ToString()); } catch (Exception e) { exceptions.Add(e); } break; case "lastbuilddate": try { channel.LastBuildDate = DateTime.Parse(elementText.ToString()); } catch (Exception e) { exceptions.Add(e); } break; case "generator": channel.Generator = elementText.ToString(); break; case "docs": channel.Docs = elementText.ToString(); break; case "ttl": try { channel.TimeToLive = int.Parse(elementText.ToString()); } catch (Exception e) { exceptions.Add(e); } break; } break; case "image": switch (childElementName) { case "url": try { image.Url = new Uri(elementText.ToString()); } catch (Exception e) { exceptions.Add(e); } break; case "title": image.Title = elementText.ToString(); break; case "link": try { image.Link = new Uri(elementText.ToString()); } catch (Exception e) { exceptions.Add(e); } break; case "description": image.Description = elementText.ToString(); break; case "width": try { image.Width = Byte.Parse(elementText.ToString()); } catch (Exception e) { exceptions.Add(e); } break; case "height": try { image.Height = Byte.Parse(elementText.ToString()); } catch (Exception e) { exceptions.Add(e); } break; } break; case "textinput": switch (childElementName) { case "title": textInput.Title = elementText.ToString(); break; case "description": textInput.Description = elementText.ToString(); break; case "name": textInput.Name = elementText.ToString(); break; case "link": try { textInput.Link = new Uri(elementText.ToString()); } catch (Exception e) { exceptions.Add(e); } break; } break; case "skipdays": if (childElementName == "day") { switch (elementText.ToString().ToLower()) { case "monday": channel.SkipDays[0] = true; break; case "tuesday": channel.SkipDays[1] = true; break; case "wednesday": channel.SkipDays[2] = true; break; case "thursday": channel.SkipDays[3] = true; break; case "friday": channel.SkipDays[4] = true; break; case "saturday": channel.SkipDays[5] = true; break; case "sunday": channel.SkipDays[6] = true; break; } } break; case "skiphours": if (childElementName == "hour") { channel.SkipHours[Byte.Parse(elementText.ToString().ToLower())] = true; } break; } break; } case XmlNodeType.Text: elementText.Append(reader.Value); break; case XmlNodeType.CDATA: elementText.Append(reader.Value); break; } } }while (readData); return(rssElement); }
/// <summary>Reads the next RssElement from the stream.</summary> /// <returns>An RSS Element</returns> /// <exception cref="InvalidOperationException">RssReader has been closed, and can not be read.</exception> /// <exception cref="System.IO.FileNotFoundException">RSS file not found.</exception> /// <exception cref="System.Xml.XmlException">Invalid XML syntax in RSS file.</exception> /// <exception cref="System.IO.EndOfStreamException">Unable to read an RssElement. Reached the end of the stream.</exception> public RssElement Read() { bool readData = false; bool pushElement = true; RssElement rssElement = null; int lineNumber = -1; int linePosition = -1; if (this.reader == null) { throw new InvalidOperationException("RssReader has been closed, and can not be read."); } do { pushElement = true; try { readData = this.reader.Read(); } catch (EndOfStreamException e) { throw new EndOfStreamException("Unable to read an RssElement. Reached the end of the stream.", e); } catch (XmlException e) { if (lineNumber != -1 || linePosition != -1) { if (this.reader.LineNumber == lineNumber && this.reader.LinePosition == linePosition) { throw this.exceptions.LastException; } } lineNumber = this.reader.LineNumber; linePosition = this.reader.LinePosition; this.exceptions.Add(e); // just add to list of exceptions and continue :) } if (readData) { string readerName = this.reader.Name.ToLower(); switch (this.reader.NodeType) { case XmlNodeType.Element: { if (this.reader.IsEmptyElement) { break; } this.elementText = new StringBuilder(); switch (readerName) { case "item": // is this the end of the channel element? (absence of </channel> before <item>) if (!this.wroteChannel) { this.wroteChannel = true; rssElement = this.channel; // return RssChannel readData = false; } this.item = new RssItem(); // create new RssItem this.channel.Items.Add(this.item); break; case "source": this.source = new RssSource(); this.item.Source = this.source; for (int i = 0; i < this.reader.AttributeCount; i++) { this.reader.MoveToAttribute(i); switch (this.reader.Name.ToLower()) { case "url": try { this.source.Url = new Uri(this.reader.Value); } catch (Exception e) { this.exceptions.Add(e); } break; } } break; case "enclosure": this.enclosure = new RssEnclosure(); this.item.Enclosure = this.enclosure; for (int i = 0; i < this.reader.AttributeCount; i++) { this.reader.MoveToAttribute(i); switch (this.reader.Name.ToLower()) { case "url": try { this.enclosure.Url = new Uri(this.reader.Value); } catch (Exception e) { this.exceptions.Add(e); } break; case "length": try { this.enclosure.Length = int.Parse(this.reader.Value); } catch (Exception e) { this.exceptions.Add(e); } break; case "type": this.enclosure.Type = this.reader.Value; break; } } break; case "guid": this.guid = new RssGuid(); this.item.Guid = this.guid; for (int i = 0; i < this.reader.AttributeCount; i++) { this.reader.MoveToAttribute(i); switch (this.reader.Name.ToLower()) { case "ispermalink": try { this.guid.PermaLink = bool.Parse(this.reader.Value); } catch (Exception e) { this.exceptions.Add(e); } break; } } break; case "category": this.category = new RssCategory(); if ((string)this.xmlNodeStack.Peek() == "channel") { this.channel.Categories.Add(this.category); } else { this.item.Categories.Add(this.category); } for (int i = 0; i < this.reader.AttributeCount; i++) { this.reader.MoveToAttribute(i); switch (this.reader.Name.ToLower()) { case "url": goto case "domain"; case "domain": this.category.Domain = this.reader.Value; break; } } break; case "channel": this.channel = new RssChannel(); this.textInput = null; this.image = null; this.cloud = null; this.source = null; this.enclosure = null; this.category = null; this.item = null; break; case "image": this.image = new RssImage(); this.channel.Image = this.image; break; case "textinput": this.textInput = new RssTextInput(); this.channel.TextInput = this.textInput; break; case "cloud": pushElement = false; this.cloud = new RssCloud(); this.channel.Cloud = this.cloud; for (int i = 0; i < this.reader.AttributeCount; i++) { this.reader.MoveToAttribute(i); switch (this.reader.Name.ToLower()) { case "domain": this.cloud.Domain = this.reader.Value; break; case "port": try { this.cloud.Port = ushort.Parse(this.reader.Value); } catch (Exception e) { this.exceptions.Add(e); } break; case "path": this.cloud.Path = this.reader.Value; break; case "registerprocedure": this.cloud.RegisterProcedure = this.reader.Value; break; case "protocol": switch (this.reader.Value.ToLower()) { case "xml-rpc": this.cloud.Protocol = RssCloudProtocol.XmlRpc; break; case "soap": this.cloud.Protocol = RssCloudProtocol.Soap; break; case "http-post": this.cloud.Protocol = RssCloudProtocol.HttpPost; break; default: this.cloud.Protocol = RssCloudProtocol.Empty; break; } break; } } break; case "rss": for (int i = 0; i < this.reader.AttributeCount; i++) { this.reader.MoveToAttribute(i); if (this.reader.Name.ToLower() == "version") { switch (this.reader.Value) { case "0.91": this.rssVersion = RssVersion.RSS091; break; case "0.92": this.rssVersion = RssVersion.RSS092; break; case "2.0": this.rssVersion = RssVersion.RSS20; break; default: this.rssVersion = RssVersion.NotSupported; break; } } } break; case "rdf": for (int i = 0; i < this.reader.AttributeCount; i++) { this.reader.MoveToAttribute(i); if (this.reader.Name.ToLower() == "version") { switch (this.reader.Value) { case "0.90": this.rssVersion = RssVersion.RSS090; break; case "1.0": this.rssVersion = RssVersion.RSS10; break; default: this.rssVersion = RssVersion.NotSupported; break; } } } break; case "geo:lat": case "geo:long": case "geo:point": case "georss:point": case "georss:elev": case "georss:radius": if (this.item.GeoPoint == null) { this.item.GeoPoint = new RssGeoPoint(); } break; } if (pushElement) { this.xmlNodeStack.Push(readerName); } break; } case XmlNodeType.EndElement: { if (this.xmlNodeStack.Count == 1) { break; } string childElementName = (string)this.xmlNodeStack.Pop(); string parentElementName = (string)this.xmlNodeStack.Peek(); switch (childElementName) // current element { // item classes case "item": rssElement = this.item; readData = false; break; case "source": this.source.Name = this.elementText.ToString(); rssElement = this.source; readData = false; break; case "enclosure": rssElement = this.enclosure; readData = false; break; case "guid": this.guid.Name = this.elementText.ToString(); rssElement = this.guid; readData = false; break; case "category": // parent is either item or channel this.category.Name = this.elementText.ToString(); rssElement = this.category; readData = false; break; // channel classes case "channel": if (this.wroteChannel) { this.wroteChannel = false; } else { this.wroteChannel = true; rssElement = this.channel; readData = false; } break; case "textinput": rssElement = this.textInput; readData = false; break; case "image": rssElement = this.image; readData = false; break; case "cloud": rssElement = this.cloud; readData = false; break; } switch (parentElementName) // parent element { case "item": switch (childElementName) { case "title": this.item.Title = this.elementText.ToString(); break; case "link": this.item.Link = new Uri(this.elementText.ToString()); break; case "description": this.item.Description = this.elementText.ToString(); break; case "author": this.item.Author = this.elementText.ToString(); break; case "comments": this.item.Comments = this.elementText.ToString(); break; case "pubdate": try { this.item.PubDate = DateTime.Parse(this.elementText.ToString()); } catch (Exception e) { try { string tmp = this.elementText.ToString(); tmp = tmp.Substring(0, tmp.Length - 5); tmp += "GMT"; this.item.PubDate = DateTime.Parse(tmp); } catch { this.exceptions.Add(e); } } break; case "geo:lat": try { this.item.GeoPoint.Lat = float.Parse(this.elementText.ToString()); } catch (Exception e) { this.exceptions.Add(e); } break; case "geo:long": try { this.item.GeoPoint.Lon = float.Parse(this.elementText.ToString()); } catch (Exception e) { this.exceptions.Add(e); } break; case "georss:point": try { string[] coords = this.elementText.ToString().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); if (coords.Length == 2) { this.item.GeoPoint.Lat = float.Parse(coords[0]); this.item.GeoPoint.Lon = float.Parse(coords[1]); } } catch (Exception e) { this.exceptions.Add(e); } break; } break; case "channel": switch (childElementName) { case "title": this.channel.Title = this.elementText.ToString(); break; case "link": try { this.channel.Link = new Uri(this.elementText.ToString()); } catch (Exception e) { this.exceptions.Add(e); } break; case "description": this.channel.Description = this.elementText.ToString(); break; case "language": this.channel.Language = this.elementText.ToString(); break; case "copyright": this.channel.Copyright = this.elementText.ToString(); break; case "managingeditor": this.channel.ManagingEditor = this.elementText.ToString(); break; case "webmaster": this.channel.WebMaster = this.elementText.ToString(); break; case "rating": this.channel.Rating = this.elementText.ToString(); break; case "pubdate": try { this.channel.PubDate = DateTime.Parse(this.elementText.ToString()); } catch (Exception e) { this.exceptions.Add(e); } break; case "lastbuilddate": try { this.channel.LastBuildDate = DateTime.Parse(this.elementText.ToString()); } catch (Exception e) { this.exceptions.Add(e); } break; case "generator": this.channel.Generator = this.elementText.ToString(); break; case "docs": this.channel.Docs = this.elementText.ToString(); break; case "ttl": try { this.channel.TimeToLive = int.Parse(this.elementText.ToString()); } catch (Exception e) { this.exceptions.Add(e); } break; } break; case "image": switch (childElementName) { case "url": try { this.image.Url = new Uri(this.elementText.ToString()); } catch (Exception e) { this.exceptions.Add(e); } break; case "title": this.image.Title = this.elementText.ToString(); break; case "link": try { this.image.Link = new Uri(this.elementText.ToString()); } catch (Exception e) { this.exceptions.Add(e); } break; case "description": this.image.Description = this.elementText.ToString(); break; case "width": try { this.image.Width = Byte.Parse(this.elementText.ToString()); } catch (Exception e) { this.exceptions.Add(e); } break; case "height": try { this.image.Height = Byte.Parse(this.elementText.ToString()); } catch (Exception e) { this.exceptions.Add(e); } break; } break; case "textinput": switch (childElementName) { case "title": this.textInput.Title = this.elementText.ToString(); break; case "description": this.textInput.Description = this.elementText.ToString(); break; case "name": this.textInput.Name = this.elementText.ToString(); break; case "link": try { this.textInput.Link = new Uri(this.elementText.ToString()); } catch (Exception e) { this.exceptions.Add(e); } break; } break; case "skipdays": if (childElementName == "day") { this.channel.SkipDays |= Day.Parse(this.elementText.ToString()); } break; case "skiphours": if (childElementName == "hour") { this.channel.SkipHours |= Hour.Parse(this.elementText.ToString()); } break; } break; } case XmlNodeType.Text: this.elementText.Append(this.reader.Value); break; case XmlNodeType.CDATA: this.elementText.Append(this.reader.Value); break; } } }while (readData); return(rssElement); }
/// <summary>Reads the next RssElement from the stream.</summary> /// <returns>An RSS Element</returns> /// <exception cref="InvalidOperationException">RssReader has been closed, and can not be read.</exception> /// <exception cref="System.IO.FileNotFoundException">RSS file not found.</exception> /// <exception cref="System.Xml.XmlException">Invalid XML syntax in RSS file.</exception> /// <exception cref="System.IO.EndOfStreamException">Unable to read an RssElement. Reached the end of the stream.</exception> public RssElement Read() { bool readData = false; bool pushElement = true; RssElement rssElement = null; int lineNumber = -1; int linePosition = -1; if (reader == null) { throw new InvalidOperationException("RssReader has been closed, and can not be read."); } do { pushElement = true; try { readData = reader.Read(); } catch (System.IO.EndOfStreamException e) { throw new System.IO.EndOfStreamException("Unable to read an RssElement. Reached the end of the stream.", e); } catch (System.Xml.XmlException e) { if (lineNumber != -1 || linePosition != -1) { if (reader.LineNumber == lineNumber && reader.LinePosition == linePosition) { throw exceptions.LastException; } } lineNumber = reader.LineNumber; linePosition = reader.LinePosition; exceptions.Add(e); // just add to list of exceptions and continue :) } if (readData) { string readerName = reader.Name.ToLower(); switch (reader.NodeType) { case XmlNodeType.Element: { if (reader.IsEmptyElement && reader.AttributeCount == 0) { break; } elementText = new StringBuilder(); switch (readerName) { case "item": // is this the end of the channel element? (absence of </channel> before <item>) if (!wroteChannel) { wroteChannel = true; rssElement = channel; // return RssChannel readData = false; } item = new RssItem(); // create new RssItem channel.Items.Add(item); break; //case "source": // source = new RssSource(); // item.Source = source; // for (int i = 0; i < reader.AttributeCount; i++) // { // reader.MoveToAttribute(i); // switch (reader.Name.ToLower()) // { // case "url": // try // { // source.Url = reader.Value; // } // catch (Exception e) // { // exceptions.Add(e); // } // break; // } // } // break; //case "pubdate": // try // { // item.PubDate = W3CDateTime.Parse(elementText.ToString()).DateTime; // } // catch (Exception e) // { // try // { // string tmp = elementText.ToString(); // tmp = tmp.Substring(0, tmp.LastIndexOf(" ")); // tmp += " GMT"; // item.PubDate = DateTime.Parse(tmp); // } // catch // { // exceptions.Add(e); // } // } // break; case "enclosure": enclosure = new RssEnclosure(); item.Enclosure = enclosure; for (int i = 0; i < reader.AttributeCount; i++) { reader.MoveToAttribute(i); switch (reader.Name.ToLower()) { case "url": try { enclosure.Url = reader.Value; } catch (Exception e) { exceptions.Add(e); } break; case "length": try { enclosure.Length = int.Parse(reader.Value); } catch (Exception) { enclosure.Length = 0; //exceptions.Add(e); } break; case "type": enclosure.Type = reader.Value; break; } } break; case "guid": guid = new RssGuid(); item.Guid = guid; for (int i = 0; i < reader.AttributeCount; i++) { reader.MoveToAttribute(i); switch (reader.Name.ToLower()) { case "ispermalink": try { guid.PermaLink = bool.Parse(reader.Value); } catch (Exception e) { exceptions.Add(e); } break; } } break; case "channel": channel = new RssChannel(); image = null; enclosure = null; item = null; break; case "image": image = new RssImage(); channel.Image = image; break; case "rss": for (int i = 0; i < reader.AttributeCount; i++) { reader.MoveToAttribute(i); if (reader.Name.ToLower() == "version") { switch (reader.Value) { case "0.91": rssVersion = RssVersion.RSS091; break; case "0.92": rssVersion = RssVersion.RSS092; break; case "2.0": rssVersion = RssVersion.RSS20; break; default: rssVersion = RssVersion.NotSupported; break; } } } break; case "rdf": for (int i = 0; i < reader.AttributeCount; i++) { reader.MoveToAttribute(i); if (reader.Name.ToLower() == "version") { switch (reader.Value) { case "0.90": rssVersion = RssVersion.RSS090; break; case "1.0": rssVersion = RssVersion.RSS10; break; default: rssVersion = RssVersion.NotSupported; break; } } } break; } if (pushElement) { xmlNodeStack.Push(readerName); } break; } case XmlNodeType.EndElement: { if (xmlNodeStack.Count == 1) { break; } string childElementName = (string)xmlNodeStack.Pop(); string parentElementName = (string)xmlNodeStack.Peek(); switch (childElementName) // current element { // item classes case "item": rssElement = item; readData = false; break; //case "source": // source.Name = elementText.ToString(); // rssElement = source; // readData = false; // break; case "enclosure": rssElement = enclosure; readData = false; break; case "guid": guid.Name = elementText.ToString(); rssElement = guid; readData = false; break; case "pubdate": rssElement = channel; readData = false; break; case "channel": if (wroteChannel) { wroteChannel = false; } else { wroteChannel = true; rssElement = channel; readData = false; } break; } switch (parentElementName) // parent element { case "item": switch (childElementName) { case "pubdate": try { item.PubDate = W3CDateTime.Parse(elementText.ToString()).DateTime; } catch (Exception e) { try { string tmp = elementText.ToString(); tmp = tmp.Substring(0, tmp.LastIndexOf(" ")); tmp += " GMT"; item.PubDate = DateTime.Parse(tmp); } catch { exceptions.Add(e); } } break; case "title": item.Title = elementText.ToString(); break; case "link": item.Link = elementText.ToString(); break; case "description": item.Description = elementText.ToString(); break; } break; case "channel": switch (childElementName) { case "title": channel.Title = elementText.ToString(); break; case "link": try { channel.Link = elementText.ToString(); } catch (Exception e) { exceptions.Add(e); } break; case "description": channel.Description = elementText.ToString(); break; case "pubdate": try { channel.PubDate = W3CDateTime.Parse(elementText.ToString()).DateTime; } catch (Exception) { if (elementText.ToString() == "") { channel.PubDate = DateTime.Now; } else { try { channel.PubDate = DateTime.Parse(elementText.ToString()); } catch (Exception e) { try { string tmp = elementText.ToString(); tmp = tmp.Substring(0, tmp.LastIndexOf(" ")); tmp += " GMT"; channel.PubDate = DateTime.Parse(tmp); } catch { exceptions.Add(e); } } } } break; case "lastbuilddate": try { channel.LastBuildDate = W3CDateTime.Parse(elementText.ToString()).DateTime; //channel.LastBuildDate = DateTime.Parse(elementText.ToString()); } catch (Exception) { if (elementText.ToString() == "") { channel.LastBuildDate = DateTime.Now; } else { try { channel.LastBuildDate = DateTime.Parse(elementText.ToString()); } catch (Exception e) { try { string tmp = elementText.ToString(); tmp = tmp.Substring(0, tmp.LastIndexOf(" ")); tmp += " GMT"; channel.LastBuildDate = DateTime.Parse(tmp); } catch { exceptions.Add(e); } } } } break; } break; case "image": switch (childElementName) { case "url": try { image.Url = elementText.ToString(); } catch (Exception e) { exceptions.Add(e); } break; case "title": image.Title = elementText.ToString(); break; case "link": try { image.Link = elementText.ToString(); } catch (Exception e) { exceptions.Add(e); } break; case "content:encoded": item.Content = elementText.ToString(); break; case "description": image.Description = elementText.ToString(); break; } break; } break; } case XmlNodeType.Text: elementText.Append(reader.Value); break; case XmlNodeType.CDATA: elementText.Append(reader.Value); break; } } }while (readData); return(rssElement); }
// Token: 0x06000699 RID: 1689 RVA: 0x00024108 File Offset: 0x00022308 public RssElement Read() { bool flag = false; bool flag2 = true; RssElement result = null; int num = -1; int num2 = -1; if (this.reader == null) { throw new InvalidOperationException("RssReader has been closed, and can not be read."); } do { flag2 = true; try { flag = this.reader.Read(); } catch (EndOfStreamException innerException) { throw new EndOfStreamException("Unable to read an RssElement. Reached the end of the stream.", innerException); } catch (XmlException exception) { if ((num != -1 || num2 != -1) && this.reader.LineNumber == num && this.reader.LinePosition == num2) { throw this.exceptions.LastException; } num = this.reader.LineNumber; num2 = this.reader.LinePosition; this.exceptions.Add(exception); } if (flag) { string text = this.reader.Name.ToLower(); XmlNodeType nodeType = this.reader.NodeType; switch (nodeType) { case XmlNodeType.Element: if (!this.reader.IsEmptyElement) { this.elementText = new StringBuilder(); string key; switch (key = text) { case "item": if (!this.wroteChannel) { this.wroteChannel = true; result = this.channel; flag = false; } this.item = new RssItem(); this.channel.Items.Add(this.item); break; case "source": this.source = new RssSource(); this.item.Source = this.source; for (int i = 0; i < this.reader.AttributeCount; i++) { this.reader.MoveToAttribute(i); string a; if ((a = this.reader.Name.ToLower()) != null && a == "url") { try { this.source.Url = new Uri(this.reader.Value); } catch (Exception exception2) { this.exceptions.Add(exception2); } } } break; case "enclosure": this.enclosure = new RssEnclosure(); this.item.Enclosure = this.enclosure; for (int j = 0; j < this.reader.AttributeCount; j++) { this.reader.MoveToAttribute(j); string a2; if ((a2 = this.reader.Name.ToLower()) != null) { if (!(a2 == "url")) { if (!(a2 == "length")) { if (!(a2 == "type")) { goto IL_3BA; } goto IL_3A4; } } else { try { this.enclosure.Url = new Uri(this.reader.Value); goto IL_3BA; } catch (Exception exception3) { this.exceptions.Add(exception3); goto IL_3BA; } } try { this.enclosure.Length = int.Parse(this.reader.Value); goto IL_3BA; } catch (Exception exception4) { this.exceptions.Add(exception4); goto IL_3BA; } IL_3A4: this.enclosure.Type = this.reader.Value; } IL_3BA :; } break; case "guid": this.guid = new RssGuid(); this.item.Guid = this.guid; for (int k = 0; k < this.reader.AttributeCount; k++) { this.reader.MoveToAttribute(k); string a3; if ((a3 = this.reader.Name.ToLower()) != null && a3 == "ispermalink") { try { this.guid.PermaLink = bool.Parse(this.reader.Value); } catch (Exception exception5) { this.exceptions.Add(exception5); } } } break; case "category": this.category = new RssCategory(); if ((string)this.xmlNodeStack.Peek() == "channel") { this.channel.Categories.Add(this.category); } else { this.item.Categories.Add(this.category); } for (int l = 0; l < this.reader.AttributeCount; l++) { this.reader.MoveToAttribute(l); string a4; if ((a4 = this.reader.Name.ToLower()) != null && (a4 == "url" || a4 == "domain")) { this.category.Domain = this.reader.Value; } } break; case "channel": this.channel = new RssChannel(); this.textInput = null; this.image = null; this.cloud = null; this.source = null; this.enclosure = null; this.category = null; this.item = null; break; case "image": this.image = new RssImage(); this.channel.Image = this.image; break; case "textinput": this.textInput = new RssTextInput(); this.channel.TextInput = this.textInput; break; case "cloud": flag2 = false; this.cloud = new RssCloud(); this.channel.Cloud = this.cloud; for (int m = 0; m < this.reader.AttributeCount; m++) { this.reader.MoveToAttribute(m); string a5; if ((a5 = this.reader.Name.ToLower()) != null) { if (!(a5 == "domain")) { if (!(a5 == "port")) { if (!(a5 == "path")) { if (a5 == "registerprocedure") { this.cloud.RegisterProcedure = this.reader.Value; goto IL_759; } if (!(a5 == "protocol")) { goto IL_759; } string a6; if ((a6 = this.reader.Value.ToLower()) != null) { if (a6 == "xml-rpc") { this.cloud.Protocol = RssCloudProtocol.XmlRpc; goto IL_759; } if (a6 == "soap") { this.cloud.Protocol = RssCloudProtocol.Soap; goto IL_759; } if (a6 == "http-post") { this.cloud.Protocol = RssCloudProtocol.HttpPost; goto IL_759; } } this.cloud.Protocol = RssCloudProtocol.Empty; goto IL_759; } } else { try { this.cloud.Port = (int)ushort.Parse(this.reader.Value); goto IL_759; } catch (Exception exception6) { this.exceptions.Add(exception6); goto IL_759; } } this.cloud.Path = this.reader.Value; } else { this.cloud.Domain = this.reader.Value; } } IL_759 :; } break; case "rss": for (int n = 0; n < this.reader.AttributeCount; n++) { this.reader.MoveToAttribute(n); if (this.reader.Name.ToLower() == "version") { string value; if ((value = this.reader.Value) != null) { if (value == "0.91") { this.rssVersion = RssVersion.RSS091; goto IL_805; } if (value == "0.92") { this.rssVersion = RssVersion.RSS092; goto IL_805; } if (value == "2.0") { this.rssVersion = RssVersion.RSS20; goto IL_805; } } this.rssVersion = RssVersion.NotSupported; } IL_805 :; } break; case "rdf": for (int num4 = 0; num4 < this.reader.AttributeCount; num4++) { this.reader.MoveToAttribute(num4); if (this.reader.Name.ToLower() == "version") { string value2; if ((value2 = this.reader.Value) != null) { if (value2 == "0.90") { this.rssVersion = RssVersion.RSS090; goto IL_897; } if (value2 == "1.0") { this.rssVersion = RssVersion.RSS10; goto IL_897; } } this.rssVersion = RssVersion.NotSupported; } IL_897 :; } break; } if (flag2) { this.xmlNodeStack.Push(text); } } break; case XmlNodeType.Attribute: break; case XmlNodeType.Text: this.elementText.Append(this.reader.Value); break; case XmlNodeType.CDATA: this.elementText.Append(this.reader.Value); break; default: if (nodeType == XmlNodeType.EndElement) { if (this.xmlNodeStack.Count != 1) { string text2 = (string)this.xmlNodeStack.Pop(); string text3 = (string)this.xmlNodeStack.Peek(); string key2; switch (key2 = text2) { case "item": result = this.item; flag = false; break; case "source": this.source.Name = this.elementText.ToString(); result = this.source; flag = false; break; case "enclosure": result = this.enclosure; flag = false; break; case "guid": this.guid.Name = this.elementText.ToString(); result = this.guid; flag = false; break; case "category": this.category.Name = this.elementText.ToString(); result = this.category; flag = false; break; case "channel": if (this.wroteChannel) { this.wroteChannel = false; } else { this.wroteChannel = true; result = this.channel; flag = false; } break; case "textinput": result = this.textInput; flag = false; break; case "image": result = this.image; flag = false; break; case "cloud": result = this.cloud; flag = false; break; } string a7; if ((a7 = text3) != null) { if (!(a7 == "item")) { if (!(a7 == "channel")) { if (a7 == "image") { goto IL_F78; } if (a7 == "textinput") { goto IL_10F1; } if (a7 == "skipdays") { goto IL_11BE; } if (!(a7 == "skiphours")) { break; } if (text2 == "hour") { this.channel.SkipHours[(int)byte.Parse(this.elementText.ToString().ToLower())] = true; break; } break; } } else { string a8; if ((a8 = text2) == null) { break; } if (a8 == "title") { this.item.Title = this.elementText.ToString(); break; } if (!(a8 == "link")) { if (a8 == "description") { this.item.Description = this.elementText.ToString(); break; } if (a8 == "author") { this.item.Author = this.elementText.ToString(); break; } if (a8 == "comments") { this.item.Comments = this.elementText.ToString(); break; } if (!(a8 == "pubdate")) { break; } try { this.item.PubDate = DateTime.Parse(this.elementText.ToString()); break; } catch (Exception exception7) { try { string text4 = this.elementText.ToString(); text4 = text4.Substring(0, text4.Length - 5); text4 += "GMT"; this.item.PubDate = DateTime.Parse(text4); } catch { this.exceptions.Add(exception7); } break; } } else { if (this.elementText.Length > 0) { this.item.Link = new Uri(this.elementText.ToString()); break; } this.item.Link = null; break; } } string key3; if ((key3 = text2) == null) { break; } if (< PrivateImplementationDetails > { 0FE034E3 - 13E4 - 4121 - 9910 - ADD6363B8EF6 }.$$method0x600068e - 3 == null) { < PrivateImplementationDetails > { 0FE034E3 - 13E4 - 4121 - 9910 - ADD6363B8EF6 }.$$method0x600068e - 3 = new Dictionary <string, int>(13)
private static RssFeed read(string url, HttpWebRequest request, RssFeed oldFeed, string username, string password) { // ***** Marked for substantial improvement RssFeed feed = new RssFeed(); RssElement element = null; Stream stream = null; Uri uri = new Uri(url); feed.url = url; string ErrorMessage = null; switch (uri.Scheme) { case "file": feed.lastModified = File.GetLastWriteTime(url); if ((oldFeed != null) && (feed.LastModified == oldFeed.LastModified)) { oldFeed.cached = true; return(oldFeed); } stream = new FileStream(url, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); break; case "https": goto case "http"; case "http": if (request == null) { request = (HttpWebRequest)WebRequest.Create(uri); } if (oldFeed != null) { request.IfModifiedSince = oldFeed.LastModified; request.Headers.Add("If-None-Match", oldFeed.ETag); request.Headers.Add("Accept-Encoding", "gzip, deflate"); } try { if (username != null && password != null) { request.Credentials = new System.Net.NetworkCredential(username, password); } HttpWebResponse response = (HttpWebResponse)request.GetResponse(); feed.lastModified = response.LastModified; feed.etag = response.Headers["ETag"]; try { if (response.ContentEncoding != "") { feed.encoding = Encoding.GetEncoding(response.ContentEncoding); } } catch {} //stream = response.GetResponseStream(); stream = GetResponseStream(response); } catch (WebException we) { if (oldFeed != null) { oldFeed.cached = true; return(oldFeed); } else { ErrorMessage = we.Message; } //else throw we; // bad } break; } if (ErrorMessage == null) { if (stream != null) { RssReader reader = null; try { reader = new RssReader(stream); //do //{ element = reader.ReadXPath(); //element = reader.Read(); if (element is RssChannel) { feed.Channels.Add((RssChannel)element); } //} //while (element != null); feed.rssVersion = reader.Version; } finally { feed.exceptions = reader.Exceptions; reader.Close(); } } else if (ErrorMessage != null) { throw new ApplicationException(ErrorMessage); } else { throw new ApplicationException("Invalid Url"); } } else { throw new ApplicationException(ErrorMessage); } return(feed); }