public Author Deserializing(string filePath)
        {
            string line = string.Empty;
            List<string> authorInfo = new List<string>();
            using (StreamReader sr = new StreamReader(filePath))
            {
                while ((line = sr.ReadLine()) != null)
                {
                    authorInfo.Add(line);
                }
            }
            Author author = new Author();
            author.Name = authorInfo[0];
            author.Email = authorInfo[1];
            for (int i = 2; i < authorInfo.Count; i++)
            {
                string[] bookInfo = authorInfo[i].Split('$');
                var book = new Book();
                book.Title = bookInfo[0];
                book.PublishDate = DateTime.Parse(bookInfo[1]);
                author.Books.Add(book);
            }

            return author;
        }
Esempio n. 2
0
 public void Serializing(Author author, string fileName)
 {
     using (StreamWriter myWriter = new StreamWriter(fileName))
     {
         mySerializer.Serialize(myWriter, author);
     }
 }
        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", "*****@*****.**", 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}");
        }
 public void SerializeAuthor(Author author, string filename)
 {
     using (System.IO.StreamWriter sw = new System.IO.StreamWriter(filename))
     {
         XmlSerializer xs = new XmlSerializer(typeof(Author));
         xs.Serialize(sw, author);
     }
 }
 public void SerializeAuthor(Author author, string filePath)
 {
     using (StreamWriter sw = new StreamWriter(filePath))
     {
         XmlSerializer xmlSerializer = new XmlSerializer(typeof(Author));
         xmlSerializer.Serialize(sw, author);
     }
 }
 public void SerializeAuthor(Author author, string filePath)
 {
     using (StreamWriter sw = new StreamWriter(filePath))
     {
         sw.WriteLine(author.Name);
         sw.WriteLine(author.Email);
         foreach (var book in author.Books)
             sw.WriteLine($"{book.Title}${book.PublishDate}");
     }
 }
 public void SerializeAuthor(Author author, string filename)
 {
     using (StreamWriter sw = new StreamWriter(filename))
     {
         sw.WriteLine(author.Name);
         sw.WriteLine(author.Email);
         foreach (var book in author.Books)
         {
             sw.WriteLine(string.Join("$", book.Title, book.PublishDate));
         }
     }
 }
Esempio n. 8
0
 static void Main(string[] args)
 {
     Book one = new Book();
     one.Title = "Think and grow rich";
     one.PublishDate = DateTime.Now;
     Book two = new Book();
     two.Title = "The virgin way";
     two.PublishDate = DateTime.Now.AddDays(23);
     List<Book> listbooks = new List<Book>() { one, two };
     Author aut = new Author("Richard Branson", listbooks);
     string path = @"C:\Users\Ivan Ivanov\Documents\Visual Studio 2015\Projects\BooksAndAuthors\targ.txt";
     aut.Deserializing(aut, path);
 }
        public void Serializing(Author author, string fileName)
        {
            List<string> authorInfo = new List<string>();
            authorInfo.Add(author.Name);
            authorInfo.Add(author.Email);
            foreach (var book in author.Books)
            {
                authorInfo.Add(string.Format("{0}${1}", book.Title, book.PublishDate.ToShortDateString()));
            }

            using (StreamWriter sw = new StreamWriter(fileName))
            {
                foreach (var line in authorInfo)
                {
                    sw.WriteLine(line);
                }
            }
        }
Esempio n. 10
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();
        }
Esempio n. 11
0
 // Using built in serialization in XML format
 public void Serializing(Author author, string path)
 {
     if (author == null)
     {
         return;
     }
     try
     {
         XmlDocument xmldoc = new XmlDocument();
         XmlSerializer serializer = new XmlSerializer(author.GetType());
         using (MemoryStream stream = new MemoryStream())
         {
             serializer.Serialize(stream, author);
             stream.Position = 0;
             xmldoc.Load(stream);
             xmldoc.Save(path);
             stream.Close();
         }
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.Message);
     }
 }
Esempio n. 12
0
        public static void Main()
        {
            // IAuthorSerializer mySerializer = new AuthorSerializer();

            IAuthorSerializer mySerializer = new CustomTextSerializer();

            List<Book> books = new List<Book>();
            books.Add(new Book() { Title = "Harry Potter", PublishDate = DateTime.Now });
            books.Add(new Book() { Title = "Intro to programming", PublishDate = DateTime.Now });
            books.Add(new Book() { Title = "Lineage", PublishDate = DateTime.Now });

            Author author = new Author() { Name = "Pesho", Books = books, Email = "*****@*****.**" };

            mySerializer.Serializing(author, "pesho.txt");
            Author pesho = mySerializer.Deserializing("pesho.txt");

            Console.WriteLine(pesho.Name);
            Console.WriteLine(pesho.Email);
            Console.WriteLine("Books Count: " + pesho.Books.Count);
            foreach (var book in pesho.Books)
            {
                Console.WriteLine("- {0} : {1}", book.Title, book.PublishDate.ToShortDateString());
            }
        }
Esempio n. 13
0
        //using Bulit in Deserialization for XML 
        //public void Deserializing(Author author, string path)
        //{
        //    try
        //    {
        //        string xmlatt = string.Empty;
        //        XmlDocument xmldoc = new XmlDocument();
        //        xmldoc.Load(path);
        //        string xmlString = xmldoc.OuterXml;
        //        using (StringReader reader = new StringReader(xmlString))
        //        {
        //            XmlSerializer selializer = new XmlSerializer(author.GetType());
        //            using (XmlReader xmlreader = new XmlTextReader(reader))
        //            {
        //                author = (Author)selializer.Deserialize(xmlreader);
        //                xmlreader.Close();
        //            }
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        Console.WriteLine(ex.Message);
        //    }
        //}

        // Using custom serialization
        //public void Serializing(Author author, string path)
        //{
        //    using (StreamWriter writer = new StreamWriter(path))
        //    {
        //        writer.WriteLine(author.Name);
        //        writer.WriteLine(author.Email);
        //        string booksandDateTime = string.Empty;
        //        foreach (var item in author.Books)
        //        {
        //            booksandDateTime = item.Title + "$" + item.PublishDate;
        //            writer.WriteLine(booksandDateTime);
        //        }
        //    }
        //}
        public void Deserializing(Author author, string path)
        {
            using (StreamReader reader = new StreamReader(path))
            {
                Author test = new Author();
                test.Books = new List<Book>();
                test.Name = reader.ReadLine();
                test.Email = reader.ReadLine();
                string[] books = new string[2];
                while (true)
                {
                    try
                    {
                        books = reader.ReadLine().Split('$');
                    }
                    catch(NullReferenceException)
                    {
                        return;
                    }
                    DateTime time = DateTime.Parse(books[1]);
                    Book book = new Book();
                    book.Title = books[0];
                    book.PublishDate = time;
                    test.Books.Add(book);

                }


            }
        }