Esempio n. 1
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();
                }
            }
        }