public DefinitionViewModel(Definition definition)
     : base()
 {
     m_def = definition;
 }
Beispiel #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"))
            {
                try
                {
                    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);
                    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,
                                IsFavorite = false,
                                IsRead = false,
                                VoteCount = voteCount,
                                Author = author,
                                AuthorInfo = authorInfo,
                                Definitions = definitions
                            };

                            newElement = w;

                            break;

                        case "contrepétrie":

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

                            newElement = c;

                            break;

                        default:
            #if DEBUG
                            Debug.WriteLine("WARNING: Ignored element " + id);
            #endif
                            continue;
                    }

                    elements.Add(newElement);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("ERROR: (parsing) " + ex);
                    continue;
                }
            }

            // Link definitions
            elements.Where(e => e is Word).ToList().ForEach(w => ((Word)w).Definitions.ForEach(d =>
               {
               d.Word = w as Word;
               }));

            Debug.WriteLine("--- WS: Parse complete (" + elements.Count + ")");

            return elements;
        }