/// <summary> /// Create book packages index. /// </summary> /// <param name="bookGroup"> /// The book Group associated with the book. /// </param> /// <param name="book"> /// The book associated with the packages /// </param> /// <returns> /// The xml document text /// </returns> public static string CreateBookPackagesIndex( BookGroup bookGroup, Book book ) { XDocument document = new XDocument( new XDeclaration( "1.0", "utf-8", null ), CreateElement( "html", null, null ) ); XElement headElement = CreateElement( "head", null, null ); XElement metaDateElemet = CreateElement( "meta", null, null ); metaDateElemet.SetAttributeValue( XName.Get( "http-equiv", string.Empty ), "Date" ); metaDateElemet.SetAttributeValue( XName.Get( "content", string.Empty ), DateTime.Now.ToString( "R", CultureInfo.InvariantCulture ) ); headElement.Add( metaDateElemet ); XElement bodyElement = CreateElement( "body", "book", null ); XElement detailsElement = CreateElement( "div", "details", null ); XElement brandingPackageElement1 = CreateElement( "a", "branding-package-link", null ); XElement brandingPackageElement2 = CreateElement( "a", "branding-package-link", null ); XElement productLinkElement = CreateElement( "a", "product-link", null ); productLinkElement.SetAttributeValue( XName.Get( "href", string.Empty ), bookGroup.CreateFileName() ); detailsElement.Add( CreateElement( "span", "name", book.Name ), CreateElement( "span", "description", book.Description ), CreateElement( "span", "vendor", "Microsoft" ), CreateElement( "span", "locale", book.Locale.Code ), brandingPackageElement1, brandingPackageElement2 ); XElement packageListElement = CreateElement( "div", "package-list", null ); foreach ( Package package in book.Packages ) { XElement packageElement = CreateElement( "div", "package", null ); XElement linkElement = CreateElement( "a", "current-link", null ); linkElement.SetAttributeValue( XName.Get( "href", string.Empty ), string.Format( CultureInfo.InvariantCulture, @"Packages\{0}", package.CreateFileName() ) ); packageElement.Add( CreateElement( "span", "name", package.Name ), CreateElement( "span", "deployed", "true" ), CreateElement( "span", "package-etag", package.Tag ), CreateElement( "span", "last-modified", package.LastModified.ToUniversalTime().ToString( "O", CultureInfo.InvariantCulture ) ), linkElement ); packageListElement.Add( packageElement ); } bodyElement.Add( detailsElement, packageListElement ); if ( document.Root != null ) { document.Root.Add( headElement, bodyElement ); } return document.ToStringWithDeclaration(); }
/// <summary> /// Find the available book groups, books, and packages for the selected language /// </summary> /// <param name="data"> /// The page data downloaded containing the book data /// </param> /// <returns> /// Collection of book groups for the help collection /// </returns> /// <exception cref="ArgumentNullException"> /// If data is null /// </exception> /// <exception cref="XmlException"> /// If there was a error processing the xml data /// </exception> /// <exception cref="InvalidOperationException"> /// If there was a error processing the xml data /// </exception> public static ICollection<BookGroup> LoadBooks( byte[] data ) { XDocument document; using ( MemoryStream stream = new MemoryStream( data ) ) { document = XDocument.Load( stream ); } if ( data == null ) { throw new ArgumentNullException( "data" ); } List<BookGroup> result = new List<BookGroup>(); if ( document.Root != null ) { IEnumerable<XElement> groups = document.Root.Elements() .Where( x => x.GetClassName() == "book-list" ) .Take( 1 ) .Single() .Elements() .Where( x => x.GetClassName() == "book-groups" ) .Take( 1 ) .Single() .Elements() .Where( x => x.GetClassName() == "book-group" ); foreach ( XElement group in groups ) { BookGroup bookGroup = new BookGroup { Name = group.GetChildClassValue( "name" ), Code = group.GetChildClassValue( "id" ), Books = new List<Book>(), Locale = new Locale { Code = "en-us" }, Description = group.GetChildClassValue( "name" ) }; result.Add( bookGroup ); IEnumerable<XElement> books = group.Elements().Where( x => x.GetClassName() == "book" ); foreach ( XElement book in books ) { XElement path = book.Elements() .Where( x => x.GetClassName() == "properties" ) .Take( 1 ) .Single() .Elements() .Where( x => x.GetClassName() == "paths" ) .Take( 1 ) .Single() .Elements() .Where( x => x.GetClassName() == "path" ) .Take( 1 ) .Single(); string bookPath = path.GetChildClassValue( "name" ).TrimStart( new[] { '\\' } ); Book b = new Book { Name = book.GetChildClassValue( "name" ), Description = book.GetChildClassValue( "description" ), Code = book.GetChildClassValue( "id" ), Packages = new List<Package>(), Locale = new Locale { Code = "en-us" }, Category = bookPath }; bookGroup.Name = bookPath; bookGroup.Books.Add( b ); IEnumerable<XElement> packages = book.Elements() .Where( x => x.GetClassName() == "packages" ) .Take( 1 ) .Single() .Elements() .Where( x => x.GetClassName() == "package" ); foreach ( XElement package in packages ) { Package p = new Package { LastModified = DateTime.Parse( package.GetChildClassValue( "last-modified" ), CultureInfo.InvariantCulture ), Link = package.GetChildClassValue( "current-link" ), Tag = package.GetChildClassValue( "package-etag" ), Name = package.GetChildClassValue( "name" ), Size = long.Parse( package.GetChildClassValue( "package-size-bytes" ), CultureInfo.InvariantCulture ) }; b.Packages.Add( p ); } } } } else { throw new XmlException( "Missing document root" ); } return result; }