public static List<string> GetSequencesAsStrings(SequenceType seq) { var allSequences = new List<string>(); if (seq != null) { if (!string.IsNullOrEmpty(seq.Name)) { string sequence = seq.Name; if (seq.Number.HasValue && seq.Number != 0) { sequence = string.Format("{0} - {1}", seq.Name, seq.Number); } allSequences.Add(sequence); } if (seq.SubSections != null) { allSequences.AddRange(seq.SubSections.SelectMany(GetSequencesAsStrings)); } } return allSequences; }
public void Load(XElement xElement) { if (xElement == null) { throw new ArgumentNullException("xElement"); } if (xElement.Name.LocalName != SequenceElementName) { throw new ArgumentException("Element of wrong type passed", "xElement"); } // read all subsecquences content.Clear(); IEnumerable<XElement> subElements = xElement.Elements(fileNameSpace + SequenceElementName); foreach (var element in subElements) { var subElement = new SequenceType(); try { subElement.Load(element); content.Add(subElement); } catch (Exception ex) { Debug.Fail(string.Format("Error reading sequence element: {0}",ex.Message)); continue; } } // read "name" attribute Name = null; XAttribute xName = xElement.Attribute("name"); if (xName != null &&(!string.IsNullOrEmpty(xName.Value))) { Name = xName.Value; } else { throw new Exception("Name attribute in sequence is required!"); } // read number attribute Number = null; XAttribute xNumber = xElement.Attribute("number"); if ((xNumber != null)&&(!string.IsNullOrEmpty(xNumber.Value))) { int value; if (int.TryParse(xNumber.Value,out value)) { Number = value; } } Language = null; XAttribute xLang = xElement.Attribute(XNamespace.Xml + "lang"); if (xLang != null && string.IsNullOrEmpty(xLang.Value)) { Language = xLang.Value; } }
internal void Load(XElement xPublishInfo) { if (xPublishInfo == null) { throw new ArgumentNullException("xPublishInfo"); } // Load book name BookTitle = null; XElement xBookName = xPublishInfo.Element(FileNameSpace + BookNameElementName); if ( xBookName != null) { BookTitle = new TextFieldType(); try { BookTitle.Load(xBookName); } catch (Exception ex) { Debug.Fail(string.Format("Error reading publisher book name : {0}", ex.Message)); } } // Load publisher Publisher = null; XElement xPublisher = xPublishInfo.Element(FileNameSpace + PublisherElementName); if (xPublisher != null) { Publisher = new TextFieldType(); try { Publisher.Load(xPublisher); } catch (Exception ex) { Debug.Fail(string.Format("Error reading publishers : {0}", ex.Message)); } } // Load city City = null; XElement xCity = xPublishInfo.Element(FileNameSpace + CityElementName); if (xCity != null) { City = new TextFieldType(); try { City.Load(xCity); } catch (Exception ex) { Debug.Fail(string.Format("Error reading publishers' City: {0}", ex.Message)); } } // Load year Year = null; XElement xYear = xPublishInfo.Element(FileNameSpace + YearElementName); if ( (xYear != null)) { int year; if ( int.TryParse( xYear.Value,out year) ) { Year = year; } } // Load ISBN ISBN = null; XElement xISBN = xPublishInfo.Element(FileNameSpace + ISBNElementName); if (xISBN != null) { ISBN = new TextFieldType(); try { ISBN.Load(xISBN); } catch (Exception ex) { Debug.Fail(string.Format("Error reading publishers' ISBN: {0}", ex.Message)); } } // Load sequence here ItemSequences.Clear(); IEnumerable<XElement> xSequences = xPublishInfo.Elements(FileNameSpace + SequenceType.SequenceElementName); foreach (var xSequence in xSequences) { var sec = new SequenceType(); try { sec.Load(xSequence); } catch (Exception ex) { Debug.Fail(string.Format("Error reading publisher sequence data: {0}", ex.Message)); continue; } } }
public void Load(XElement xTitleInfo) { if ( xTitleInfo == null ) { throw new ArgumentNullException("xTitleInfo"); } // Load genres _genres.Clear(); IEnumerable<XElement> xGenres = xTitleInfo.Elements(FileNameSpace + GenreElementName); foreach ( XElement xGenre in xGenres ) { if ( (xGenre != null) ) { var genre = new TitleGenreType {Genre = xGenre.Value}; XAttribute xMatch = xGenre.Attribute("match"); if (xMatch != null && !string.IsNullOrEmpty(xMatch.Value)) { int percentage; if (int.TryParse(xMatch.Value,out percentage)) { genre.Match = percentage; } } _genres.Add(genre); } } // Load authors _bookAuthors.Clear(); IEnumerable<XElement> xAuthors = xTitleInfo.Elements(FileNameSpace + AuthorType.AuthorElementName); foreach (XElement xAuthor in xAuthors ) { var author = new AuthorItem { Namespace = FileNameSpace }; try { author.Load(xAuthor); _bookAuthors.Add(author); } catch (Exception ex) { Debug.Fail(string.Format("Error reading author: {0}",ex.Message)); continue; } } // Load Title BookTitle = null; XElement xBookTitle = xTitleInfo.Element(FileNameSpace + BookTitleElementName); if (xBookTitle != null) { BookTitle = new TextFieldType(); try { BookTitle.Load(xBookTitle); } catch (Exception ex) { Debug.Fail(string.Format("Error reading book title: {0}", ex.Message)); } } // Load Annotation Annotation = null; XElement xAnnotation = xTitleInfo.Element(FileNameSpace + AnnotationElementName); if (xAnnotation != null) { Annotation = new AnnotationItem(); try { Annotation.Load(xAnnotation); } catch (Exception ex) { Debug.Fail(string.Format("Error reading annotation: {0}", ex.Message)); } } // Load keywords Keywords = null; XElement xKeywords = xTitleInfo.Element(FileNameSpace + KeywordsElementName); if (xKeywords != null) { Keywords = new TextFieldType(); try { Keywords.Load(xKeywords); } catch (Exception ex) { Debug.Fail(string.Format("Error reading keywords: {0}", ex.Message)); } } // Load Book date BookDate = null; XElement xBookDate = xTitleInfo.Element(FileNameSpace + DateItem.Fb2DateElementName); if (xBookDate != null) { BookDate = new DateItem(); try { BookDate.Load(xBookDate); } catch (Exception ex) { Debug.Fail(string.Format("Error reading book date: {0}", ex.Message)); } } Cover = null; // we should load coverpage images here but no use for them as for now XElement xCoverPage = xTitleInfo.Element(FileNameSpace + CoverPageElementName); if ( xCoverPage != null) { Cover = new CoverPage{Namespace = FileNameSpace}; try { Cover.Load(xCoverPage); } catch (Exception ex) { Debug.Fail(string.Format("Error reading cover: {0}", ex.Message)); } } // Load Language Language = null; XElement xLanguage = xTitleInfo.Element(FileNameSpace + LanguageElementName); if ( (xLanguage != null)) { Language = xLanguage.Value; } else { Debug.Write("Language not specified in title section"); } // Load source language SrcLanguage = null; XElement xSrcLanguage = xTitleInfo.Element(FileNameSpace + SourceLanguageElementName); if ( (xSrcLanguage != null) ) { SrcLanguage = xSrcLanguage.Value; } // Load translators _translators.Clear(); IEnumerable<XElement> xTranslators = xTitleInfo.Elements(FileNameSpace + AuthorType.TranslatorElementName); foreach ( XElement xTranslator in xTranslators ) { var translator = new TranslatorItem { Namespace = FileNameSpace }; try { translator.Load(xTranslator); _translators.Add(translator); } catch (Exception ex) { Debug.Fail(string.Format("Error reading translator: {0}", ex.Message)); continue; } } // Load sequences ItemSequences.Clear(); IEnumerable<XElement> xSequences = xTitleInfo.Elements(FileNameSpace + SequenceType.SequenceElementName); foreach (var xSequence in xSequences) { var sec = new SequenceType{ Namespace = FileNameSpace }; try { sec.Load(xSequence); ItemSequences.Add(sec); } catch (Exception ex) { Debug.Fail(string.Format("Error reading sequence data: {0}",ex.Message)); continue; } } }