public BookViewModel(Book model) { Model = model; AuthorViewModel = new AuthorViewModel(model.Author); if(!AuthorViewModel.PublishedBooks.Contains(this)) AuthorViewModel.PublishedBooks.Add(this); PageViewModels = new ObservableCollection<PageViewModel>(); foreach (Page page in model.Pages) { PageViewModels.Add(new PageViewModel(page)); } PageViewModels.CollectionChanged += new NotifyCollectionChangedEventHandler(PageViewModels_CollectionChanged); AddPageCommand = new AddPageCommand(this); RemovePageCommand = new RemovePageCommand(this); }
/// <summary> /// XML Serialization of a book. /// </summary> /// <param name="book"></param> public static void SerializeBook(Book book) { XmlSerializer serializer = new XmlSerializer(typeof(Book)); TextWriter textWriter = new StreamWriter("serializedBook.xml"); serializer.Serialize(textWriter, book); textWriter.Close(); }
/// <summary> /// Search an XML file. /// </summary> /// <param name="title"></param> static void FindBookByTitle(string title) { Console.WriteLine("Searching for a book named \""+title+"\".."); #region search using child element values // Element and Elements only search for the children (they don't go further) var query = from book in file.Element("Books").Elements("Book") // Elements() returns all children where book.Element("Title").Value == title select book; // NullReferenceException wenn es eines der Elemente nicht gibt => fangen! #endregion search using child element values #region search using descendant axes // Descendants looks in the children, the children's children and so on (all elements with the specified name) query = from book in file.Descendants("Book") where book.Element("Title").Value == title select book; #endregion search using descendant axis #region search for parent of the title // Parent looks up one level query = from bookTitle in file.Descendants("Title") where bookTitle.Value.Equals(title) select bookTitle.Parent; #endregion search for parent of the title #region more navigation options // Ancestors looks all the way up to the root // ElementsBeforeSelf and ElementsAfterSelf looks at the siblings #endregion more navigation options XElement oneBook = query.SingleOrDefault(); // returns the _single_ result, null if more than one result, exception if none if (oneBook != null) { // Book found: Console.WriteLine("Book found."); myBook = new Book(oneBook.Element("Author").Value, oneBook.Element("Title").Value, oneBook.Attribute("ReleaseDate").Value ); Console.WriteLine(myBook.Author + " : " + myBook.Title + " (" + myBook.ReleaseDate + ")"); } else{ Console.WriteLine("Book not found."); } }