static void Main()
        {
            List<Book> books = new List<Book>()
            {
                new Book("The Power Of Now", new DateTime(1997, 03, 01)),
                new Book("C# for Dummies", new DateTime(2010, 01, 01)),
                new Book("Book: The book", new DateTime(2016, 01, 30))
            };

            Author author = new Author("Peter Parker", "[email protected]", books);

            // example #1
            string filePath = @"C:\Users\Nicky\Desktop\author.xml";
            XMLAuthorSerializer serializer = new XMLAuthorSerializer();
            serializer.SerializeAuthor(author, filePath);

            // example #2
            string filePath2 = @"C:\Users\Nicky\Desktop\customAuthor.txt";
            CustomAuthorSerializer customSerializer = new CustomAuthorSerializer();
            customSerializer.SerializeAuthor(author, filePath2);

            // example #3
            string filePath3 = @"C:\Users\Nicky\Desktop\authorToDeserialize.txt";
            Author deserializedAuthor = customSerializer.DeserializeAuthor(filePath3);
            Console.WriteLine(deserializedAuthor.Name);
            Console.WriteLine(deserializedAuthor.Email);
            foreach (var book in deserializedAuthor.Books)
                Console.WriteLine($"{book.Title} - {book.PublishDate}");
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            var author1 = new Author("authorName", "authorEmail",
                new List<Book>()
                {
                    new Book("Book-title1", DateTime.Now),
                    new Book("Book-title2", DateTime.Now.AddDays(-465)),
                    new Book("Book-title3", DateTime.Now.AddMonths(-45))
                });

            XmlAuthorSerializer xas = new XmlAuthorSerializer();
            xas.SerializeAuthor(author1, "author1.xml");

            Console.WriteLine("--------------------------------------");
            Console.WriteLine("----Build-in XML Serializer output----");
            Console.WriteLine("--------------------------------------");
            Console.WriteLine(File.ReadAllText("author1.xml"));

            Author author2 = xas.DeserializeAuthor("author1.xml");

            CustomAuthorSerializer bas = new CustomAuthorSerializer();
            bas.SerializeAuthor(author2, "author2.txt");
            Console.WriteLine("-------------------------------------");
            Console.WriteLine("-----Custom author serialization-----");
            Console.WriteLine("-------------------------------------");
            Console.WriteLine(File.ReadAllText("author2.txt"));
            Console.WriteLine("--------------------------------------");
            var author3 = bas.DeserializeAuthor("author2.txt");

            Console.WriteLine("---Used author---");
            Console.WriteLine($"Name: {author3.Name}");
            Console.WriteLine($"Email: {author3.Email}");
            Console.WriteLine("Books: ");
            Console.WriteLine(string.Join(Environment.NewLine, author3.Books));
            Console.WriteLine("--------------------------------------");
            Console.ReadKey();
        }