public virtual XDocument Generate()
        {
            var mainDocument = new HTMLDocument(Compatibility);
            GenerateHead();
            GenerateBody();
            var encoding = new UTF8Encoding();
            foreach (var file in _styles)
            {
                IHTMLItem styleElement;
                if (EmbedStyles)
                {
                    var styleElementEntry = new Style(Compatibility);
                    styleElement = styleElementEntry;
                    styleElementEntry.Type.Value = CSSFile.MediaType.GetAsSerializableString();
                    try
                    {
                        using (var outStream = new MemoryStream())
                        {
                            file.Write(outStream);
                            styleElementEntry.InternalTextItem.Text = encoding.GetString(outStream.ToArray());
                        }
                    }
                    catch
                    {
                        // ignored
                    }
                }
                else
                {
                    var cssStyleSheet = new Link(Compatibility);
                    styleElement = cssStyleSheet;
                    cssStyleSheet.Relation.Value = "stylesheet";
                    cssStyleSheet.Type.Value = file.GetMediaType().GetAsSerializableString();
                    cssStyleSheet.HRef.Value = file.PathInEPUB.GetRelativePath(FileEPubInternalPath, FlatStructure);
                }
                HeadElement.Add(styleElement);
            }

            mainDocument.RootHTML.Add(HeadElement);

            mainDocument.RootHTML.Add(BodyElement);

            if (!mainDocument.RootHTML.IsValid())
            {
               throw new Exception("Document content is not valid");
            }

            var titleElm = new Title(Compatibility);
            titleElm.InternalTextItem.Text = InternalPageTitle;
            HeadElement.Add(titleElm);

            _generatedCodeXDocument =  mainDocument.Generate();
            Durty = false;
            return _generatedCodeXDocument;
        }
        private void CreateNAVDocument(XDocument contentDocument)
        {
            var html = new XElement(WWWNamespaces.XHTML + "html");
            html.Add(new XAttribute(XNamespace.Xmlns + "epub", EPubNamespaces.OpsNamespace));
            contentDocument.Add(html);

            var head = new XElement(WWWNamespaces.XHTML + "head");
            html.Add(head);
            var meta = new XElement(WWWNamespaces.XHTML + "meta");
            meta.Add(new XAttribute("charset","utf-8"));
            head.Add(meta);
            var title = new XElement(WWWNamespaces.XHTML + "title");
            if (string.IsNullOrEmpty(PageTitle))
            {
                title.Value = "Table of Contents";
            }
            else
            {
                title.Value = WebUtility.HtmlEncode(PageTitle) + " - Table of Contents";
            }
            head.Add(title);
            foreach (var file in _styles)
            {
                var cssStyleSheet = new Link(HTMLElementType.HTML5);
                cssStyleSheet.Relation.Value = "stylesheet";
                cssStyleSheet.Type.Value = file.GetMediaType().GetAsSerializableString();
                cssStyleSheet.HRef.Value = file.PathInEPUB.GetRelativePath(NAVFilePath, FlatStructure);
                head.Add(cssStyleSheet.Generate());
            }

            var body = new XElement(WWWNamespaces.XHTML + "body");
            body.Add(new XAttribute("class","nav_body"));
            html.Add(body);

            var navElement = _documentNavigationMap.GenerateXMLMap();
            if (navElement != null)
            {
                body.Add(navElement);
            }

            var landmarksElement = _landmarks.GenerateXMLMap();
            if (landmarksElement != null)
            {
                body.Add(landmarksElement);
            }
        }