示例#1
0
        public Element ToElement()
        {
            AuthorDao autdao = new AuthorDao(cvDb);
            var authors = autdao.GetAuthors();

            Element e = null;

            if (Type == "word")
            {
                Word w = new Word();

                if (string.IsNullOrEmpty(Details1) == false && string.IsNullOrEmpty(Content1) == false)
                {
                    w.Definitions.Add(new Definition()
                    {
                        Id = IdDef1,
                        Content = Content1,
                        Details = Details1,
                        WordId = w.Id
                    });
                }
                if (string.IsNullOrEmpty(Details2) == false && string.IsNullOrEmpty(Content2) == false)
                {
                    w.Definitions.Add(new Definition()
                    {
                        Id = IdDef2,
                        Content = Content2,
                        Details = Details2,
                        WordId = w.Id
                    });
                }
                if (string.IsNullOrEmpty(Details3) == false && string.IsNullOrEmpty(Content3) == false)
                {
                    w.Definitions.Add(new Definition()
                    {
                        Id = IdDef3,
                        Content = Content3,
                        Details = Details3,
                        WordId = w.Id
                    });
                }

                e = w;
            }
            else
            {
                Contrepeterie ctp = new Contrepeterie();

                ctp.Content = Content;
                ctp.Solution = Solution;

                e = ctp;
            }

            e.Author = authors.Where(a => a.Id == AuthorId).FirstOrDefault();
            e.Id = ElementId;
            e.Title = Title;
            e.Date = Date;
            e.FavoriteCount = VoteCount;

            return e;
        }
示例#2
0
        /// <summary>
        /// Parse the XML response of the webservice
        /// </summary>
        /// <param name="xml"></param>
        /// <returns></returns>
        private List<Element> parseResponse(string xml)
        {
            List<Element> elements = new List<Element>();

            XDocument xdoc = XDocument.Parse(xml);

            foreach (var element in xdoc.Descendants("element"))
            {
                int id = Convert.ToInt32(element.Element("id").Value);
                string type = element.Element("type").Value;
                DateTime date = Convert.ToDateTime(element.Element("date").Value);
                string title = element.Element("title").Value.Replace(@"\", "");
                int voteCount = Convert.ToInt32(element.Element("voteCount").Value);

                if (element.Element("author") == null && element.Element("authorInfo") == null)
                {
                    continue;
                }

                string author = element.Element("author").Value;
                string authorInfo = element.Element("authorInfo").Value;

                Element newElement = null;

                switch (type)
                {
                    case "mot":

                        List<Definition> definitions = new List<Definition>();
                        foreach (XElement defXml in element.Element("definitions").Elements("definition"))
                        {
                            Definition newDef = new Definition()
                            {
                                Details = defXml.Element("details").Value.Replace(@"\", ""),
                                Content = defXml.Element("content").Value.Replace(@"\", ""),
                            };

                            definitions.Add(newDef);
                        }

                        Word w = new Word()
                        {
                            Id = id,
                            Date = date,
                            Title = title,
                            FavoriteCount = voteCount,
                            Definitions = definitions
                        };
                        w.Author.Info = authorInfo;
                        w.Author.Name = author;

                        newElement = w;

                        break;

                    case "contrepétrie":

                        Contrepeterie c = new Contrepeterie()
                        {
                            Id = id,
                            Date = date,
                            Title = title,
                            FavoriteCount = voteCount,
                            Content = element.Element("content").Value.Replace(@"\", ""),
                            Solution = element.Element("solution").Value.Replace(@"\", ""),
                        };
                        c.Author.Info = authorInfo;
                        c.Author.Name = author;

                        newElement = c;

                        break;

                    default:
                        continue;
                }

                elements.Add(newElement);
            }

            return elements;
        }