/// <summary> /// Initializes a new instance of the RssChannel class and set default values. /// </summary> public RssChannel() { Cloud = new RssCloud(); Image = new RssImage(); TextInput = new RssTextInput(); _items = new RssItemCollection(this); SetVersion("2.0"); // this.PubDate = DateTime.Now; this.LastBuildDate = PubDate; this.Language = System.Globalization.CultureInfo.CurrentCulture; }
/// <summary> /// Parser use XmlTextReader as input for data /// </summary> protected virtual void Parse(XmlReader xmlTextReader) { try { // initalize PropertyInfo propertyInfo = null; bool supressRead = false; // try to read the rss header xmlTextReader.MoveToContent(); if (xmlTextReader.Name == "rss") SetVersion(xmlTextReader.GetAttribute("version")); else SetVersion(""); xmlTextReader.Read(); // process channel while (!xmlTextReader.EOF) { // if no ReadInnerXml() call was done, read if (!supressRead) xmlTextReader.Read(); // Move to content xmlTextReader.MoveToContent(); // set default for next loop supressRead = false; // if (xmlTextReader.NodeType == XmlNodeType.Element) { if (xmlTextReader.Name == "item") { if (xmlTextReader.IsEmptyElement) continue; // add new item to item collection Items.Add(new RssItem(xmlTextReader)); } // image has sub elements else if (xmlTextReader.Name == "image") { if (xmlTextReader.IsEmptyElement) continue; Image = new RssImage(xmlTextReader); } // image has sub elements else if (xmlTextReader.Name == "textInput") { if (xmlTextReader.IsEmptyElement) continue; TextInput = new RssTextInput(xmlTextReader); } else if (xmlTextReader.Name == "cloud") { if (!xmlTextReader.HasAttributes) continue; Cloud = new RssCloud(xmlTextReader); } else if (xmlTextReader.Name == "skipDays") { if (xmlTextReader.IsEmptyElement) continue; // find related property by name propertyInfo = GetType().GetProperty(xmlTextReader.Name, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance); if (propertyInfo != null) { // find days and parse SkipDays enum int result = 0; while (!(xmlTextReader.Name == "skipDays" && xmlTextReader.NodeType == XmlNodeType.EndElement)) { xmlTextReader.Read(); if (xmlTextReader.NodeType != XmlNodeType.Element) continue; xmlTextReader.Read(); result += (int)Enum.Parse(typeof(SkipDays), xmlTextReader.Value, true); } // set value propertyInfo.SetValue(this, Enum.ToObject(typeof(SkipDays), result), null); } } else if (xmlTextReader.Name == "skipHours") { if (xmlTextReader.IsEmptyElement) continue; // find related property by name propertyInfo = GetType().GetProperty(xmlTextReader.Name, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance); if (propertyInfo != null) { // read all hours elements ArrayList list = new ArrayList(); while (!(xmlTextReader.Name == "skipHours" && xmlTextReader.NodeType == XmlNodeType.EndElement)) { xmlTextReader.Read(); if (xmlTextReader.NodeType != XmlNodeType.Element) continue; xmlTextReader.Read(); list.Add(Convert.ToInt32(xmlTextReader.Value)); } // set value propertyInfo.SetValue(this, list.ToArray(typeof(int)), null); } } // items is the item collection, skip if specified else if (xmlTextReader.Name.ToLower() == "items") { } // otherwise fill channel element else { supressRead = XmlSerializationUtil.DecodeXmlTextReaderValue(this, xmlTextReader); } } } } catch (Exception e) { throw e; } finally { if (xmlTextReader != null) xmlTextReader.Close(); } }