Esempio n. 1
0
 public static string GetXmlAttributeValue(XParseElement node, string attName)
 {
     if (node != null)
     {
         XParseAttribute att = node.Attribute(XParseName.Get(attName));
         if (att != null)
         {
             return(att.Value);
         }
     }
     return(string.Empty);
 }
Esempio n. 2
0
        protected override LocationIDSearchResult ConvertResult(Base.ConnectionInfo connInfo, System.IO.Stream stream, Base.SettingsBase settings)
        {
            List <LocationIDData> lst = new List <LocationIDData>();
            XParseDocument        doc = MyHelper.ParseXmlDocument(stream);

            XParseElement[] results = XPath.GetElements("//loc", doc);
            foreach (XParseElement locNode in results)
            {
                LocationIDData loc = new LocationIDData();
                loc.Name = locNode.Value;
                XParseAttribute att = locNode.Attribute(XParseName.Get("id"));
                if (att != null)
                {
                    loc.ID = att.Value;
                }
                lst.Add(loc);
            }
            return(new LocationIDSearchResult(lst.ToArray()));
        }
        internal PortfolioInfoResult ConvertHtml(XParseDocument doc)
        {
            XParseElement        resultsNode = XPath.GetElement("//div[@id=\"yfi-main\"]/div/div[2]/form/div/table/tbody", doc);
            List <PortfolioInfo> lst         = new List <PortfolioInfo>();

            if (resultsNode != null)
            {
                foreach (XParseElement trNode in resultsNode.Elements())
                {
                    XParseElement a = XPath.GetElement("/td[2]/a", trNode);
                    if (a != null)
                    {
                        string          name = a.Value;
                        XParseAttribute att  = a.Attribute(XParseName.Get("href"));
                        if (att != null)
                        {
                            string id = att.Value.Split(';')[0].Split(new string[] { "/" }, StringSplitOptions.RemoveEmptyEntries)[1];
                            lst.Add(new PortfolioInfo(name, id));
                        }
                    }
                }
            }
            return(new PortfolioInfoResult(lst.ToArray()));
        }
Esempio n. 4
0
        internal Holding[] ConvertHtmlDoc(XParseDocument doc, string portfolioID)
        {
            if (doc != null)
            {
                System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("en-US");
                List <Holding>  lst      = new List <Holding>();
                XParseElement[] rowNodes = XPath.GetElements("//form[@action=\"/portfolio/" + portfolioID + "/save-sort\"]/div/table/tbody/tr", doc);
                if (rowNodes != null)
                {
                    XPath inputPath = new XPath("//input");
                    foreach (XParseElement trNode in rowNodes)
                    {
                        Holding         h = new Holding();
                        XParseName      valueName = XParseName.Get("value");
                        XParseName      nmeName = XParseName.Get("name");
                        XParseElement[] inputs = inputPath.GetElements(trNode);
                        int             i; double d;
                        int             dd = 0, md = 0, yd = 0;
                        foreach (XParseElement inputNode in inputs)
                        {
                            XParseAttribute valueAtt = inputNode.Attribute(valueName);
                            XParseAttribute nameAtt  = inputNode.Attribute(nmeName);
                            if (valueAtt != null && nameAtt != null)
                            {
                                switch (nameAtt.Value)
                                {
                                case "yfi_pf_order[]":
                                    if (int.TryParse(valueAtt.Value, System.Globalization.NumberStyles.Any, MyHelper.ConverterCulture, out i))
                                    {
                                        h.Order = i;
                                    }
                                    break;

                                case "yfi_pf_lot[]":
                                    if (int.TryParse(valueAtt.Value, System.Globalization.NumberStyles.Any, MyHelper.ConverterCulture, out i))
                                    {
                                        h.Lot = i;
                                    }
                                    break;

                                case "yfi_pf_symbol[]":
                                    h.SetID(valueAtt.Value);
                                    break;

                                case "yfi_pf_price_paid[]":
                                    if (double.TryParse(valueAtt.Value, System.Globalization.NumberStyles.Any, MyHelper.ConverterCulture, out d))
                                    {
                                        h.PricePaid = d;
                                    }
                                    break;

                                case "yfi_pf_shares_owned[]":
                                    if (int.TryParse(valueAtt.Value, System.Globalization.NumberStyles.Any, MyHelper.ConverterCulture, out i))
                                    {
                                        h.Shares = i;
                                    }
                                    break;

                                case "yfi_pf_commission[]":
                                    if (double.TryParse(valueAtt.Value, System.Globalization.NumberStyles.Any, MyHelper.ConverterCulture, out d))
                                    {
                                        h.Commission = d;
                                    }
                                    break;

                                case "yfi_pf_comment[]":
                                    h.Notes = valueAtt.Value;
                                    break;

                                case "yfi_pf_lowlimit[]":
                                    if (double.TryParse(valueAtt.Value, System.Globalization.NumberStyles.Any, MyHelper.ConverterCulture, out d))
                                    {
                                        h.LowLimit = d;
                                    }
                                    break;

                                case "yfi_pf_highlimit[]":
                                    if (double.TryParse(valueAtt.Value, System.Globalization.NumberStyles.Any, MyHelper.ConverterCulture, out d))
                                    {
                                        h.HighLimit = d;
                                    }
                                    break;

                                case "yfi_pf_trade_date_day[]":
                                    if (int.TryParse(valueAtt.Value, System.Globalization.NumberStyles.Any, MyHelper.ConverterCulture, out i))
                                    {
                                        dd = i;
                                    }
                                    break;

                                case "yfi_pf_trade_date_month[]":
                                    if (int.TryParse(valueAtt.Value, System.Globalization.NumberStyles.Any, MyHelper.ConverterCulture, out i))
                                    {
                                        md = i;
                                    }
                                    break;

                                case "yfi_pf_trade_date_year[]":
                                    if (int.TryParse(valueAtt.Value, System.Globalization.NumberStyles.Any, MyHelper.ConverterCulture, out i))
                                    {
                                        yd = i;
                                    }
                                    break;
                                }
                            }
                            if ((dd != 0) && (md != 0) && (yd != 0) && !(dd == DateTime.Today.Day && md == DateTime.Today.Month && yd == DateTime.Today.Year))
                            {
                                h.TradeDate = new DateTime(yd, md, dd);
                            }
                        }
                        lst.Add(h);
                    }
                }
                return(lst.ToArray());
            }
            else
            {
                return(null);
            }
        }
Esempio n. 5
0
        private void ConvertHtml(XParseDocument doc, AsyncArgs args)
        {
            string fap = string.Empty;

            if (args.Settings.FormActionPattern != string.Empty)
            {
                fap = "//form[@" + args.Settings.FormActionPattern + "]";
            }
            else
            {
                fap = "//form[@action=\"" + args.Settings.RefererUrlPart + "\"]";
            }
            XParseElement formNode = new XPath(fap, true).GetElement(doc);

            if (formNode != null)
            {
                XParseElement[] inputNodes = XPath.GetElements("//input", formNode);
                foreach (XParseElement inp in inputNodes)
                {
                    XParseAttribute nameAtt = inp.Attribute(XParseName.Get("name"));
                    if (nameAtt != null)
                    {
                        string          value    = string.Empty;
                        XParseAttribute valueAtt = inp.Attribute(XParseName.Get("value"));
                        if (valueAtt != null && valueAtt.Value != string.Empty)
                        {
                            value = valueAtt.Value;
                        }

                        if (nameAtt.Value.Contains("challenge"))
                        {
                            args.IsLoginChallenge = true;
                        }

                        if (nameAtt.Value.Contains("crumb"))
                        {
                            args.Settings.Account.SetCrumb(valueAtt.Value);
                        }
                        else
                        {
                            if (args.Settings.SearchForWebForms != null)
                            {
                                foreach (string name in args.Settings.SearchForWebForms)
                                {
                                    if (name == nameAtt.Value)
                                    {
                                        if (args.Settings.AdditionalWebForms != null && args.Settings.AdditionalWebForms.Count > 0)
                                        {
                                            for (int i = 0; i < args.Settings.AdditionalWebForms.Count; i++)
                                            {
                                                if (args.Settings.AdditionalWebForms[i].Key == nameAtt.Value)
                                                {
                                                    args.Settings.AdditionalWebForms[i] = new KeyValuePair <string, string>(nameAtt.Value, valueAtt.Value);
                                                    break;
                                                }
                                            }
                                        }
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        protected override BondScreenerResult ConvertResult(ConnectionInfo connInfo, System.IO.Stream stream, SettingsBase settings)
        {
            List <BondScreenerData> results = new List <BondScreenerData>();

            System.Globalization.CultureInfo convCulture = new System.Globalization.CultureInfo("en-US");
            XParseDocument doc = MyHelper.ParseXmlDocument(stream);

            XParseElement[] resultsNodes = XPath.GetElements("//results", doc);
            if (resultsNodes.Length == 1)
            {
                XParseElement resultsNode = resultsNodes[0];
                foreach (XParseElement trNode in resultsNode.Elements())
                {
                    XParseAttribute classAtt = trNode.Attribute(XParseName.Get("class"));
                    if (classAtt != null)
                    {
                        if (classAtt.Value == "yfnc_tabledata1")
                        {
                            BondScreenerData res = new BondScreenerData();
                            int index            = 0;
                            foreach (XParseElement tdNode in trNode.Elements())
                            {
                                switch (index)
                                {
                                case 0:
                                    XParseElement fontNode = null;    //tdNode.FirstNode;
                                    switch (fontNode.Value)
                                    {
                                    case "Treas":
                                        res.Type = BondType.Treasury;
                                        break;

                                    case "Zero":
                                        res.Type = BondType.TreasuryZeroCoupon;
                                        break;

                                    case "Corp":
                                        res.Type = BondType.Corporate;
                                        break;

                                    case "Muni":
                                        res.Type = BondType.Municipal;
                                        break;
                                    }
                                    break;

                                case 1:
                                    XParseElement aNode = null;     // tdNode.FirstNode.FirstNode;
                                    fontNode = null;
                                    //fontNode = tdNode.LastNode.FirstNode;
                                    res.Issue = new Link(fontNode.Value.Replace("\n", ""), new Uri("http://reports.finance.yahoo.com" + aNode.Attribute(XParseName.Get("href")).Value));
                                    break;

                                case 2:
                                    fontNode = null;
                                    //fontNode = tdNode.FirstNode.FirstNode;
                                    double t;
                                    if (double.TryParse(fontNode.Value, System.Globalization.NumberStyles.Any, convCulture, out t))
                                    {
                                        res.Price = t;
                                    }
                                    break;

                                case 3:
                                    fontNode = null;
                                    //fontNode = tdNode.FirstNode.FirstNode;
                                    if (double.TryParse(fontNode.Value, System.Globalization.NumberStyles.Any, convCulture, out t))
                                    {
                                        res.CouponInPercent = t;
                                    }
                                    break;

                                case 4:
                                    XParseElement nobrNode = null;    // tdNode.FirstNode.FirstNode.FirstNode;
                                    DateTime      d;
                                    if (System.DateTime.TryParseExact(nobrNode.Value, "dd-MMM-yyyy", convCulture, System.Globalization.DateTimeStyles.None, out d))
                                    {
                                        res.Maturity = d;
                                    }
                                    break;

                                case 5:
                                    fontNode = null;
                                    //fontNode = tdNode.FirstNode.FirstNode;
                                    if (double.TryParse(fontNode.Value, System.Globalization.NumberStyles.Any, convCulture, out t))
                                    {
                                        res.YieldToMaturityInPercent = t;
                                    }
                                    break;

                                case 6:
                                    fontNode = null;
                                    //fontNode = tdNode.FirstNode.FirstNode;
                                    if (double.TryParse(fontNode.Value, System.Globalization.NumberStyles.Any, convCulture, out t))
                                    {
                                        res.CurrentYieldInPercent = t;
                                    }
                                    break;

                                case 7:
                                    fontNode = null;
                                    //fontNode = tdNode.FirstNode;
                                    foreach (Rating r in Enum.GetValues(typeof(Rating)))
                                    {
                                        if (r.ToString() == fontNode.Value)
                                        {
                                            res.FitchRating = r;
                                            break;     // TODO: might not be correct. Was : Exit For
                                        }
                                    }

                                    break;

                                case 8:
                                    fontNode = null;
                                    //fontNode = tdNode.FirstNode;
                                    res.Callable = Convert.ToBoolean((fontNode.Value == "Yes" ? true : false));
                                    break;
                                }
                                index += 1;
                            }
                            results.Add(res);
                        }
                    }
                }
            }
            return(new BondScreenerResult(results.ToArray()));
        }
Esempio n. 7
0
        internal Portfolio ConvertHtmlDoc(XParseDocument doc)
        {
            if (doc != null)
            {
                List <IID>              lstIDs     = new List <IID>();
                PortfolioInfo           ownPf      = null;
                List <PortfolioInfo>    portfolios = new List <PortfolioInfo>();
                List <PortfolioDataRow> dt         = new List <PortfolioDataRow>();
                string selectedView = string.Empty;
                List <PortfolioColumnType> lstColumns     = new List <PortfolioColumnType>();
                List <PortfolioColumnType> lstViewColumns = new List <PortfolioColumnType>();
                List <string> views    = new List <string>();
                XParseElement mainNode = XPath.GetElement("//div[@id=\"yfi-main\"]", doc);
                if (mainNode != null)
                {
                    XParseElement portfoliosNode = XPath.GetElement("/div[@class=\"yfi_dropdown_select\"]/div[@class=\"bd\"]/form/div/select", mainNode);
                    if (portfoliosNode != null)
                    {
                        foreach (XParseElement childNode in portfoliosNode.Elements())
                        {
                            if (childNode.Name.LocalName == "option" && childNode.HasElements)
                            {
                                string id   = childNode.Attribute(XParseName.Get("value")).Value;
                                string name = childNode.Value;
                                portfolios.Add(new PortfolioInfo(name, id));
                                if (childNode.Attribute(XParseName.Get("selected")) != null && childNode.Attribute(XParseName.Get("selected")).Value == "selected")
                                {
                                    ownPf = portfolios[portfolios.Count - 1];
                                }
                            }
                        }
                    }

                    XParseElement[] viewNodes = XPath.GetElements("/div[@class=\"yfi_tablist\"]/div/ul/li", mainNode);
                    if (viewNodes.Length > 0)
                    {
                        foreach (XParseElement viewNode in viewNodes)
                        {
                            if (!viewNode.Value.Contains("Detailed") && !viewNode.Value.Contains("Add Custom View"))
                            {
                                views.Add(viewNode.Value);
                                if (XPath.GetElement("/strong", viewNode) != null)
                                {
                                    selectedView = viewNode.Value;
                                }
                            }
                        }
                    }

                    XParseElement tableNode = XPath.GetElement("/div[@class=\"yfi_module yfi-quotes-table\"]/div[2]/table", mainNode);
                    if (tableNode != null)
                    {
                        XParseElement[] columnCells = XPath.GetElements("/thead/tr/th", tableNode);

                        foreach (XParseElement thNode in columnCells)
                        {
                            XParseAttribute colNameAtt = thNode.Attribute(XParseName.Get("class"));
                            if (colNameAtt != null && colNameAtt.Value.StartsWith("col-"))
                            {
                                string colName = colNameAtt.Value.Split(' ')[0].Replace("col-", "");
                                Nullable <PortfolioColumnType> colType = this.GetColumnType(colName);

                                if (colType != null)
                                {
                                    int subColumnCount = Convert.ToInt32(thNode.Attribute(XParseName.Get("colspan")).Value);
                                    if (subColumnCount == 1)
                                    {
                                        lstColumns.Add(colType.Value);
                                        lstViewColumns.Add(colType.Value);
                                    }
                                    else
                                    {
                                        Nullable <PortfolioColumnType>[] addCT = new Nullable <PortfolioColumnType> [subColumnCount];
                                        switch (colType)
                                        {
                                        case PortfolioColumnType.change:
                                            if (subColumnCount == 2)
                                            {
                                                addCT[0] = PortfolioColumnType.change; addCT[1] = PortfolioColumnType.percent_change;
                                            }
                                            lstViewColumns.Add(PortfolioColumnType.change_and_percent);
                                            break;

                                        case PortfolioColumnType.time:
                                            if (subColumnCount == 2)
                                            {
                                                addCT[0] = PortfolioColumnType.time; addCT[1] = PortfolioColumnType.price;
                                            }
                                            lstViewColumns.Add(PortfolioColumnType.price_and_time);
                                            break;

                                        case PortfolioColumnType.day_low:
                                            if (subColumnCount == 2)
                                            {
                                                addCT[0] = PortfolioColumnType.day_low; addCT[1] = PortfolioColumnType.day_high;
                                            }
                                            lstViewColumns.Add(PortfolioColumnType.day_range);
                                            break;

                                        case PortfolioColumnType.day_value_change:
                                            if (subColumnCount == 2)
                                            {
                                                addCT[0] = PortfolioColumnType.day_value_change; addCT[1] = PortfolioColumnType.day_value_percent_change;
                                            }
                                            lstViewColumns.Add(PortfolioColumnType.day_value_change_and_percent);
                                            break;

                                        case PortfolioColumnType.fiftytwo_week_low:
                                            if (subColumnCount == 2)
                                            {
                                                addCT[0] = PortfolioColumnType.fiftytwo_week_low; addCT[1] = PortfolioColumnType.fiftytwo_week_high;
                                            }
                                            lstViewColumns.Add(PortfolioColumnType.fiftytwo_week_range);
                                            break;

                                        case PortfolioColumnType.pre_mkt_time:
                                            PortfolioColumnType t = PortfolioColumnType.pre_mkt_price_and_time;
                                            if (subColumnCount >= 2)
                                            {
                                                addCT[0] = PortfolioColumnType.pre_mkt_time; addCT[1] = PortfolioColumnType.pre_mkt_price;
                                            }
                                            if (subColumnCount == 4)
                                            {
                                                addCT[2] = PortfolioColumnType.after_mkt_time; addCT[3] = PortfolioColumnType.after_mkt_price; t = PortfolioColumnType.pre_after_mkt_price_and_time;
                                            }
                                            lstViewColumns.Add(t);
                                            break;

                                        case PortfolioColumnType.after_mkt_time:
                                            if (subColumnCount == 2)
                                            {
                                                addCT[0] = PortfolioColumnType.after_mkt_time; addCT[1] = PortfolioColumnType.after_mkt_price;
                                            }
                                            lstViewColumns.Add(PortfolioColumnType.pre_after_mkt_price_and_time);
                                            break;

                                        case PortfolioColumnType.pre_mkt_change:
                                            if (subColumnCount == 2)
                                            {
                                                addCT[0] = PortfolioColumnType.pre_mkt_change; addCT[1] = PortfolioColumnType.pre_mkt_percent_change;
                                            }
                                            lstViewColumns.Add(PortfolioColumnType.pre_mkt_change_and_percent);
                                            break;

                                        case PortfolioColumnType.after_mkt_change:
                                            if (subColumnCount == 2)
                                            {
                                                addCT[0] = PortfolioColumnType.after_mkt_change; addCT[1] = PortfolioColumnType.after_mkt_percent_change;
                                            }
                                            lstViewColumns.Add(PortfolioColumnType.after_mkt_change_and_percent);
                                            break;

                                        case PortfolioColumnType.holdings_gain:
                                            if (subColumnCount == 2)
                                            {
                                                addCT[0] = PortfolioColumnType.holdings_gain; addCT[1] = PortfolioColumnType.holdings_percent_gain;
                                            }
                                            lstViewColumns.Add(PortfolioColumnType.holdings_gain_and_percent);
                                            break;
                                        }

                                        for (int i = 0; i < addCT.Length; i++)
                                        {
                                            if (addCT[i] != null)
                                            {
                                                lstColumns.Add(addCT[i].Value);
                                            }
                                            else
                                            {
                                                System.Diagnostics.Debug.WriteLine(colName);
                                            }
                                        }
                                    }
                                }
                            }
                        }

                        XParseElement[] tableCells = XPath.GetElements("/tbody/tr", tableNode);
                        foreach (XParseElement trNode in tableCells)
                        {
                            XParseElement[] enm = MyHelper.EnumToArray(trNode.Elements());
                            if (enm.Length > 0)
                            {
                                PortfolioDataRow r = new PortfolioDataRow();
                                for (int i = 0; i < enm.Length; i++)
                                {
                                    XParseElement   tdNode     = enm[i];
                                    XParseAttribute colNameAtt = tdNode.Attribute(XParseName.Get("class"));
                                    if (colNameAtt != null && colNameAtt.Value.StartsWith("col-"))
                                    {
                                        XParseElement[] tdEnm   = MyHelper.EnumToArray(tdNode.Elements());
                                        string          colName = colNameAtt.Value.Split(' ')[0].Replace("col-", "");
                                        if (i < lstColumns.Count && lstColumns[i].ToString() == colName &&
                                            colName != "delete" &&
                                            (tdNode.Attribute(XParseName.Get("class")) != null && !tdNode.Attribute(XParseName.Get("class")).Value.StartsWith("col-delete")) &&
                                            tdEnm.Length > 0 && tdEnm[0].Name.LocalName == "span" && tdEnm[0].Attribute(XParseName.Get("class")) != null && tdEnm[0].Attribute(XParseName.Get("class")).Value == "wrapper")
                                        {
                                            string cellTxt = tdNode.Value;
                                            r[lstColumns[i]] = cellTxt;
                                            if (lstColumns[i] == PortfolioColumnType.symbol)
                                            {
                                                lstIDs.Add(new SimpleID(cellTxt));
                                            }
                                        }
                                    }
                                }
                                dt.Add(r);
                            }
                        }
                    }
                }

                return(new Portfolio(ownPf, lstIDs.ToArray(), dt.ToArray(), selectedView, views.ToArray(), lstColumns.ToArray(), lstViewColumns.ToArray(), portfolios.ToArray()));
            }
            else
            {
                return(null);
            }
        }
Esempio n. 8
0
        public static Feed ToFeed(XParseElement channel)
        {
            if (channel.Name.LocalName.ToLower() == "channel")
            {
                Feed feed = new Feed();
                foreach (XParseElement prpNode in channel.Elements())
                {
                    try
                    {
                        switch (prpNode.Name.LocalName.ToLower())
                        {
                        case "category":
                            feed.Category      = new Category();
                            feed.Category.Name = prpNode.Value;
                            if (MyHelper.EnumToArray(prpNode.Attributes()).Length == 1)
                            {
                                feed.Category.Domain = new Uri(prpNode.Attribute(XParseName.Get("domain")).Value);
                            }
                            break;

                        case "cloud":
                            feed.Cloud = new Cloud();
                            foreach (XParseAttribute att in prpNode.Attributes())
                            {
                                switch (att.Name.LocalName.ToLower())
                                {
                                case "domain":
                                    feed.Cloud.Domain = new Uri(att.Value);
                                    break;

                                case "path":
                                    feed.Cloud.Path = att.Value;
                                    break;

                                case "registerprocedure":
                                    feed.Cloud.RegisterProcedure = att.Value;
                                    break;

                                case "protocol":
                                    feed.Cloud.Protocol = att.Value;
                                    break;

                                case "port":
                                    int n;
                                    if (int.TryParse(att.Value, out n))
                                    {
                                        feed.Cloud.Port = n;
                                    }
                                    break;
                                }
                            }

                            break;

                        case "copyright":
                            feed.Copyright = prpNode.Value;
                            break;

                        case "description":
                            feed.Description = prpNode.Value;
                            break;

                        case "docs":
                            feed.Documentation = new Uri(prpNode.Value);
                            break;

                        case "generator":
                            feed.Generator = prpNode.Value;
                            break;

                        case "link":
                            feed.Link = new Uri(prpNode.Value);
                            break;

                        case "language":
                            feed.Language = new System.Globalization.CultureInfo(prpNode.Value);
                            break;

                        case "lastbuilddate":
                            feed.LastBuildDate = RFC822DateFromString(prpNode.Value);
                            break;

                        case "managingeditor":
                            feed.ManagingEditor = Rss2MailToMailAddress(prpNode.Value);
                            break;

                        case "name":
                            feed.Name = prpNode.Value;
                            break;

                        case "image":
                            feed.Image = new Image();
                            foreach (XParseElement nodeChild in prpNode.Elements())
                            {
                                switch (nodeChild.Name.LocalName.ToLower())
                                {
                                case "url":
                                    feed.Image.URL = new Uri(nodeChild.Value);
                                    break;

                                case "link":
                                    feed.Image.Link = new Uri(nodeChild.Value);
                                    break;

                                case "title":
                                    feed.Image.Title = nodeChild.Value;
                                    break;

                                case "description":
                                    feed.Image.Description = nodeChild.Value;
                                    break;

                                case "width":
                                    int n;
                                    if (int.TryParse(nodeChild.Value, out n))
                                    {
                                        feed.Image.Width = n;
                                    }
                                    break;

                                case "height":
                                    if (int.TryParse(nodeChild.Value, out n))
                                    {
                                        feed.Image.Height = n;
                                    }
                                    break;
                                }
                            }

                            break;

                        case "item":
                            FeedItem rssItem = ToFeedItem(prpNode);
                            if (rssItem != null)
                            {
                                feed.Items.Add(rssItem);
                            }
                            break;

                        case "pubdate":
                            feed.PublishDate = RFC822DateFromString(prpNode.Value);
                            break;

                        case "rating":
                            feed.Rating = prpNode.Value;
                            break;

                        case "skiphours":
                            List <int> lst1 = new List <int>();
                            foreach (XParseElement nodeChild in prpNode.Elements())
                            {
                                if (nodeChild.Name.LocalName.ToLower() == "hour")
                                {
                                    int @int = 0;
                                    if (int.TryParse(nodeChild.Value, out @int))
                                    {
                                        lst1.Add(@int);
                                    }
                                }
                            }

                            feed.Skiphours = lst1.ToArray();
                            break;

                        case "skipdays":
                            List <DayOfWeek> lst2 = new List <DayOfWeek>();
                            foreach (XParseElement nodeChild in prpNode.Elements())
                            {
                                if (nodeChild.Name.LocalName.ToLower() == "day")
                                {
                                    for (int i = 0; i <= (int)DayOfWeek.Saturday; i++)
                                    {
                                        if (((DayOfWeek)i).ToString().ToUpper() == nodeChild.Value.ToUpper())
                                        {
                                            lst2.Add((DayOfWeek)i);
                                            break;     // TODO: might not be correct. Was : Exit For
                                        }
                                    }
                                }
                            }

                            feed.Skipdays = lst2.ToArray();
                            break;

                        case "textinput":
                            feed.TextInput = new TextInputBox();
                            foreach (XParseElement nodeChild in prpNode.Elements())
                            {
                                switch (nodeChild.Name.LocalName.ToLower())
                                {
                                case "name":
                                    feed.TextInput.Name = nodeChild.Value;
                                    break;

                                case "link":
                                    feed.TextInput.Link = new Uri(nodeChild.Value);
                                    break;

                                case "title":
                                    feed.TextInput.Title = nodeChild.Value;
                                    break;

                                case "description":
                                    feed.TextInput.Description = nodeChild.Value;
                                    break;
                                }
                            }


                            break;

                        case "title":
                            feed.Title = prpNode.Value;
                            break;

                        case "ttl":
                            feed.Ttl = Convert.ToInt32(prpNode.Value);

                            break;

                        case "webmaster":
                            feed.Webmaster = Rss2MailToMailAddress(prpNode.Value);
                            break;

                        default:
                            break;
                            //Debug.WriteLine(node.Name)
                        }
                    }
                    catch (Exception ex)
                    {
                    }
                }
                return(feed);
            }
            else
            {
                return(null);
            }
        }
Esempio n. 9
0
        public static FeedItem ToFeedItem(XParseElement item)
        {
            if (item.Name.LocalName == "item")
            {
                FeedItem rssItem = new FeedItem();
                foreach (XParseElement itemNode in item.Elements())
                {
                    try
                    {
                        switch (itemNode.Name.LocalName.ToLower())
                        {
                        case "title":
                            rssItem.Title = itemNode.Value;
                            break;

                        case "link":
                            rssItem.Link = new Uri(itemNode.Value);
                            break;

                        case "description":
                            rssItem.Description = itemNode.Value;
                            break;

                        case "author":
                            rssItem.Author = itemNode.Value;
                            break;

                        case "category":
                            rssItem.Category      = new Category();
                            rssItem.Category.Name = itemNode.Value;
                            if (MyHelper.EnumToArray(itemNode.Attributes()).Length == 1)
                            {
                                rssItem.Category.Domain = new Uri(itemNode.Attribute(XParseName.Get("domain")).Value);
                            }
                            break;

                        case "comments":
                            rssItem.Comments = new Uri(itemNode.Value);
                            break;

                        case "enclosure":
                            rssItem.Enclosure     = new Enclosure();
                            rssItem.Enclosure.Url = new Uri(itemNode.Attribute(XParseName.Get("url")).Value);
                            long l;
                            if (long.TryParse(itemNode.Attribute(XParseName.Get("length")).Value, out l))
                            {
                                rssItem.Enclosure.Length = l;
                            }
                            rssItem.Enclosure.Type = itemNode.Attribute(XParseName.Get("type")).Value;
                            break;

                        case "guid":
                            rssItem.GUID    = new GUID();
                            rssItem.GUID.ID = itemNode.Value;
                            if (MyHelper.EnumToArray(itemNode.Attributes()).Length == 1)
                            {
                                rssItem.GUID.IsPermaLink = Convert.ToBoolean(itemNode.Attribute(XParseName.Get("isPermaLink")).Value);
                            }
                            break;

                        case "pubdate":
                            rssItem.PublishDate = RFC822DateFromString(itemNode.Value);
                            break;

                        case "insertdate":
                            rssItem.InsertDate = RFC822DateFromString(itemNode.Value);
                            break;

                        case "source":
                            rssItem.Source       = new Source();
                            rssItem.Source.Title = itemNode.Value;
                            rssItem.Source.Url   = new Uri(itemNode.Attribute(XParseName.Get("url")).Value);
                            break;

                        default:
                            break;
                            //Debug.WriteLine(itemNode.Name.LocalName)
                        }
                    }
                    catch (Exception ex)
                    {
                    }
                }
                return(rssItem);
            }
            return(null);
        }