예제 #1
0
        /// <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 )
        {
            if ( data == null )
            {
                throw new ArgumentNullException( "data" );
            }

            XDocument document;
            List<BookGroup> result = new List<BookGroup>();

            using ( MemoryStream stream = new MemoryStream( data ) )
            {
                document = XDocument.Load( stream );
            }

            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
                                        {
                                            Code = group.GetChildClassValue( "id" ),
                                            Name = group.GetChildClassValue( "name" ),
                                            Vendor = group.GetChildClassValue( "vendor" ),
                                            Books = new List<Book>()
                                        };

                    result.Add( bookGroup );

                    IEnumerable<XElement> books = group.Elements().Where( x => x.GetClassName() == "book" );
                    foreach ( XElement book in books )
                    {
                        Book b = new Book
                        {
                            Code = book.GetChildClassValue( "id" ),
                            Locale = new Locale { Code = book.GetChildClassValue( "locale" ) },
                            //Local = book.GetChildClassValue( "locale" ),
                            Name = book.GetChildClassValue( "name" ),
                            Description = book.GetChildClassValue( "description" ),
                            BrandingPackageName = book.GetChildClassValue( "BrandingPackageName" ),
                            Paths = new List<MSDNPath>(),
                            Packages = new List<Package>()
                        };

                        IEnumerable<XElement> paths =
                            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();

                        foreach (XElement path in paths)
                        {
                            MSDNPath p = new MSDNPath
                                            {
                                                Languages = path.GetChildClassValue( "languages" ),
                                                Membership = path.GetChildClassValue( "membership" ),
                                                Name = path.GetChildClassValue( "name" ),
                                                //Priority = path.GetChildClassValue( "priority" ),
                                                Priority = long.Parse( path.GetChildClassValue( "priority" ), CultureInfo.InvariantCulture ),
                                                //SkuId = path.GetChildClassValue( "skuId" ),
                                                SkuId = long.Parse( path.GetChildClassValue( "skuId" ), CultureInfo.InvariantCulture ),
                                                SkuName = path.GetChildClassValue( "skuName" )
                                            };

                                            if ( p.SkuName == "Enterprise" )
                                                p.SkuId = 3000;
                                            else if ( p.SkuName == "Professional" )
                                                p.SkuId = 2000;
                                            else if ( p.SkuName == "Standard" )
                                                p.SkuId = 1000;
                                            else if ( p.SkuName == "Express" )
                                                p.SkuId = 500;

                            b.Paths.Add(p);

                            string bookPath = p.Name.TrimStart(new[] { '\\' });
                            b.Category = bookPath;
                        }

                        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 pa = new Package
                                            {
                                                PackageType = package.GetChildClassValue( "packageType" ),
                                                PackageFormat = package.GetChildClassValue( "packageFormat" ),
                                                Name = package.GetChildClassValue( "name" ),
                                                Deployed = package.GetChildClassValue( "deployed" ),
                                                LastModified = DateTime.Parse( package.GetChildClassValue( "last-modified" ), CultureInfo.InvariantCulture ),
                                                Tag = package.GetChildClassValue( "package-etag" ),
                                                Link = package.GetChildClassAttributeValue( "current-link", "href" ),
                                                Size = long.Parse( package.GetChildClassValue( "package-size-bytes" ), CultureInfo.InvariantCulture ),// unused
                                                UncompressedSize = long.Parse( package.GetChildClassValue( "package-size-bytes-uncompressed" ), CultureInfo.InvariantCulture ),// unused
                                                ConstituentLink = package.GetChildClassAttributeValue( "package-constituent-link", "href" )
                                            };

                            b.Packages.Add( pa );
                        }

                        bookGroup.Books.Add(b);
                    }
                }

            }
            else
            {
                throw new XmlException( "Missing document root" );
            }

            return result;
        }