コード例 #1
0
        /// <summary>
        /// Opens the cached feed file to read the last updated date.
        /// </summary>
        /// <param name="rssFilename"></param>
        /// <returns>The DateTime when it was last updated, or DateTime.MinValue if never updated.</returns>
        public DateTime GetLastUpdate(string rssFilename)
        {
            DateTime dtLastUpdate = DateTime.MinValue;                  // Never

            if (!System.IO.File.Exists(rssFilename))
            {
                return(dtLastUpdate);
            }

            XmlTextReader xmlReader = null;

            try
            {
                string strElementName;
                XPath  xPath = new XPath();

                xmlReader = new XmlTextReader(rssFilename);
                while (xmlReader.Read())
                {
                    XmlNodeType type = xmlReader.NodeType;
                    if (type == XmlNodeType.Element)
                    {
                        strElementName = xmlReader.Name;
                        if (!xmlReader.IsEmptyElement && xPath.AddElement(strElementName) == "/rss/channel")
                        {
                            if (strElementName == "comshak:lastUpdate")
                            {
                                if (!xmlReader.Read() || (xmlReader.NodeType != XmlNodeType.Text))
                                {
                                    continue;
                                }
                                dtLastUpdate = DateTime.Parse(xmlReader.Value);
                                break;
                            }
                        }
                    }
                    else if (type == XmlNodeType.EndElement)
                    {
                        strElementName = xmlReader.Name;
                        xPath.RemoveElement(strElementName);
                    }
                }
            }
            finally
            {
                if (xmlReader != null)
                {
                    xmlReader.Close();
                }
            }
            return(dtLastUpdate);
        }
コード例 #2
0
        // Read the items from the rss channel stored on the local storage, and add them to this collection.
        // Be sure to keep the date from the existing item (copy it to the current element).
        public void Merge()
        {
            XmlTextReader xmlReader = null;

            try
            {
                if (!File.Exists(m_strFileName))
                {
                    Write();
                    return;
                }

                string  strElementName;
                XPath   xPath     = new XPath();
                RssItem localItem = null;

                xmlReader = new XmlTextReader(m_strFileName);
                while (xmlReader.Read())
                {
                    XmlNodeType type = xmlReader.NodeType;
                    if (type == XmlNodeType.Element)
                    {
                        strElementName = xmlReader.Name;
                        if (xmlReader.IsEmptyElement)                           // Check this first!
                        {
                            if (strElementName == "enclosure")
                            {
                                localItem.ReadEnclosure(xmlReader);
                            }
                        }
                        else if (xPath.AddElement(strElementName) == "/rss/channel/item")
                        {
                            if (!xmlReader.Read())
                            {
                                continue;
                            }
                            XmlNodeType nt = xmlReader.NodeType;
                            if ((nt != XmlNodeType.Text) && (nt != XmlNodeType.CDATA))
                            {
                                continue;
                            }
                            if (localItem == null)
                            {
                                localItem = new RssItem();
                            }
                            localItem.ReadElement(strElementName, xmlReader.Value, nt);
                        }
                    }
                    else if (type == XmlNodeType.EndElement)
                    {
                        strElementName = xmlReader.Name;
                        xPath.RemoveElement(strElementName);
                        if ((strElementName == "item") && xPath.Equals("/rss/channel") && (localItem != null))
                        {
                            AddIfNew(localItem);
                            localItem = null;
                        }
                    }
                }
                xmlReader.Close();
                xmlReader = null;
                Write();
            }
            catch (Exception ex)
            {
                Utils.DbgOutExc("RssChannel::Merge()", ex);
            }
            finally
            {
                if (xmlReader != null)
                {
                    xmlReader.Close();
                }
            }
        }