Пример #1
0
 public void TestXMLSerializer(BookModel sd)
 {
     using (var ms = new MemoryStream())
     {
         // time in milliseconds
         double serTime = TestMethod(() =>
             XMLSerializationHelper.Serialize(ms, sd));
         long size = ms.Length;
         ms.Position = 0;
         double deSerTime = TestMethod(() =>
             XMLSerializationHelper.Deserialize(ms, typeof(BookModel)));
     }
 }
Пример #2
0
        public static List<BookModel> ParseBooksUsingXmlReader(Stream sr)
        {
            var books = new List<BookModel>();

            XmlReader xmlr = XmlReader.Create(sr);

            BookModel book = null;
            while (xmlr.Read())
            {
                if (xmlr.NodeType == XmlNodeType.Element)
                {
                    if (xmlr.Name == "book")
                    {
                        book = new BookModel();
                        // parsing id attribute
                        book.Id = long.Parse(xmlr["id"]);
                    }
                    else if (xmlr.Name == "title")
                    {
                        if (book != null)
                            book.Title = xmlr.ReadElementContentAsString();
                    }
                    else if (xmlr.Name == "description")
                    {
                        if (book != null)
                            book.Description = xmlr.ReadElementContentAsString();
                    }
                    else if (xmlr.Name == "year")
                    {
                        if (book != null)
                            book.Year = xmlr.ReadElementContentAsInt();

                        // add book to collection
                        books.Add(book);
                    }

                }
            }

            return books;
        }