Exemplo n.º 1
0
        public static List <SearchImage> HandleSearchImagesCallBack(string value)
        {
            try
            {
                List <SearchImage> imgs = new List <SearchImage>();
                JsonConvert.DeserializeObject(value);

                value = value.Remove(0, 9);
                string[] entries = value.Split(new string[] { ",\"showsAds\":false}},", ",\"showsAds\":true}},", ",\"is_album\":false},", ",\"is_album\":true}," }, StringSplitOptions.RemoveEmptyEntries);
                if (entries.Length == 0 || !entries[entries.Length - 1].Contains("success"))
                {
                    Console.WriteLine("Can't handle search images.");
                    return(imgs);
                }
                for (int i = 1; i < entries.Length - 1; i++)
                {
                    string tmp = entries[i] + "}";
                    XmlDictionaryReader jsonReader = JsonReaderWriterFactory.CreateJsonReader(Encoding.UTF8.GetBytes(tmp),
                                                                                              new System.Xml.XmlDictionaryReaderQuotas());
                    XElement    root = XElement.Load(jsonReader);
                    SearchImage img  = searchImageArray(root);
                    if (img == null)
                    {
                        img = new SearchImage(root.XPathSelectElement("//id").Value,
                                              root.XPathSelectElement("//title").Value,
                                              root.XPathSelectElement("//description").Value,
                                              root.XPathSelectElement("//datetime").Value,
                                              root.XPathSelectElement("//link").Value,
                                              root.XPathSelectElement("//favorite").Value,
                                              root.XPathSelectElement("//type").Value);
                    }
                    imgs.Add(img);
                }
                return(imgs);
            }
            catch { return(null); }
        }
Exemplo n.º 2
0
        private static SearchImage searchImageArray(XElement root)
        {
            string      link  = "";
            bool        found = false;
            SearchImage img   = new SearchImage();

            try
            {
                IEnumerable <XElement> items = root.XPathSelectElement("//images").Elements();
                foreach (XElement item in items)
                {
                    foreach (XElement attr in items.Elements())
                    {
                        if (attr.Name == "link" && !attr.Value.Contains("."))
                        {
                            break;
                        }
                        if (attr.Name == "link")
                        {
                            img.Source = attr.Value;
                            found      = true;
                        }
                        if (attr.Name == "id")
                        {
                            img.Id = attr.Value;
                        }
                        if (attr.Name == "title")
                        {
                            img.Title = attr.Value;
                        }
                        if (attr.Name == "description")
                        {
                            img.Description = attr.Value;
                        }
                        if (attr.Name == "datetime")
                        {
                            img.Time = new DateTime(Convert.ToInt32(attr.Value));
                        }
                        if (attr.Name == "favorite")
                        {
                            if (attr.Value == "true")
                            {
                                img.Favorite = true;
                            }
                            else
                            {
                                img.Favorite = false;
                            }
                        }
                        if (attr.Name == "type")
                        {
                            img.Type = attr.Value;
                        }
                    }
                    if (found)
                    {
                        break;
                    }
                }
            }
            catch
            {
                found = false;
            }
            return(img);
        }