private void ApplyRootNamespaces(XPathNavigator docNavigator, string key)
        {
            XPathNodeIterator iterator = docNavigator.Select(_rootTopicSelector);

            if (iterator == null || iterator.Count == 0)
            {
                return;
            }

            foreach (XPathNavigator navigator in iterator)
            {
                if (navigator.MoveToAttribute("item", String.Empty))
                {
                    navigator.SetValue("namespaceTopicTitle");
                }
            }

            RootNamespaceItem rootItem = _tocExcludedNamespaces[key];

            if (rootItem == null)
            {
                return;
            }
            string tocExcludedTopic = rootItem.TocExcludedTopic;

            if (String.IsNullOrEmpty(tocExcludedTopic))
            {
                return;
            }
            if (!_tocExcludedDocuments.ContainsKey(tocExcludedTopic))
            {
                return;
            }
            XmlDocument sourceDocument = _tocExcludedDocuments[tocExcludedTopic];

            if (sourceDocument == null)
            {
                return;
            }

            XPathNavigator posNavigator = docNavigator.SelectSingleNode(
                _tocPositionSelector);

            XmlWriter writer = null;

            if (_rootContentsAfter)
            {
                writer = posNavigator.InsertAfter();
            }
            else
            {
                writer = posNavigator.InsertBefore();
            }
            if (writer == null)
            {
                return;
            }

            // We will now copy the namespace documentations from the forged
            // namespace to this document...
            XPathNavigator sourceNavigator = sourceDocument.CreateNavigator();

            // 1. For the classes...
            XPathNavigator hNavigator = sourceNavigator.SelectSingleNode(
                _tocClassHSelector);
            XPathNavigator divNavigator = sourceNavigator.SelectSingleNode(
                _tocClassDivSelector);

            if (hNavigator != null && divNavigator != null)
            {
                writer.WriteNode(hNavigator, true);
                writer.WriteNode(divNavigator, true);
            }

            // 2. For the structures...
            hNavigator   = sourceNavigator.SelectSingleNode(_tocStructHSelector);
            divNavigator = sourceNavigator.SelectSingleNode(_tocStructDivSelector);

            if (hNavigator != null && divNavigator != null)
            {
                writer.WriteNode(hNavigator, true);
                writer.WriteNode(divNavigator, true);
            }

            // 3. For the interfaces...
            hNavigator   = sourceNavigator.SelectSingleNode(_tocInterfaceHSelector);
            divNavigator = sourceNavigator.SelectSingleNode(_tocInterfaceDivSelector);

            if (hNavigator != null && divNavigator != null)
            {
                writer.WriteNode(hNavigator, true);
                writer.WriteNode(divNavigator, true);
            }

            // 4. For the delegates...
            hNavigator   = sourceNavigator.SelectSingleNode(_tocDelegateHSelector);
            divNavigator = sourceNavigator.SelectSingleNode(_tocDelegateDivSelector);

            if (hNavigator != null && divNavigator != null)
            {
                writer.WriteNode(hNavigator, true);
                writer.WriteNode(divNavigator, true);
            }

            // 5. For the enumerations...
            hNavigator   = sourceNavigator.SelectSingleNode(_tocEnumHSelector);
            divNavigator = sourceNavigator.SelectSingleNode(_tocEnumDivSelector);

            if (hNavigator != null && divNavigator != null)
            {
                writer.WriteNode(hNavigator, true);
                writer.WriteNode(divNavigator, true);
            }

            writer.Close();
        }
        private void ParseRootNamespace(XPathNavigator configuration)
        {
            XPathNavigator navigator = configuration.SelectSingleNode(
                "rootNamespaces");

            if (navigator == null)
            {
                return;
            }

            _rootId = navigator.GetAttribute("id", String.Empty);

            string rootNamespacesFile = navigator.GetAttribute(
                "source", String.Empty);

            if (!File.Exists(rootNamespacesFile))
            {
                return;
            }

            _tocExcludedNamespaces = new Dictionary <string, RootNamespaceItem>(
                StringComparer.OrdinalIgnoreCase);
            _tocExcludedDocuments = new Dictionary <string, XmlDocument>(
                StringComparer.OrdinalIgnoreCase);

            _rootTopicSelector = XPathExpression.Compile(
                "//include[@item='rootTopicTitle']");

            string tempText = String.Empty;

            using (XmlReader reader = XmlReader.Create(rootNamespacesFile))
            {
                while (reader.Read())
                {
                    if (reader.NodeType == XmlNodeType.Element)
                    {
                        if (String.Equals(reader.Name, "topic",
                                          StringComparison.OrdinalIgnoreCase))
                        {
                            string topicId = reader.GetAttribute("id");
                            if (!String.IsNullOrEmpty(topicId))
                            {
                                tempText = reader.GetAttribute("isTocExcluded");
                                if (!String.IsNullOrEmpty(tempText) &&
                                    tempText.Equals("true", StringComparison.OrdinalIgnoreCase))
                                {
                                    tempText = reader.GetAttribute("tocExcludedTopic");
                                    if (tempText != null && tempText.Length > 2)
                                    {
                                        RootNamespaceItem rootItem = new RootNamespaceItem();
                                        rootItem.IsExcluded       = true;
                                        rootItem.TopicId          = topicId;
                                        rootItem.TocExcludedTopic = tempText;
                                        rootItem.File             = reader.GetAttribute("topicFile");

                                        _tocExcludedNamespaces[topicId] = rootItem;
                                        _tocExcludedDocuments[tempText] = null;
                                    }
                                    else
                                    {
                                        _tocExcludedNamespaces[topicId] = null;
                                    }
                                }
                                else
                                {
                                    _tocExcludedNamespaces[topicId] = null;
                                }
                            }
                        }
                        else if (String.Equals(reader.Name, "topics",
                                               StringComparison.OrdinalIgnoreCase))
                        {
                            tempText = reader.GetAttribute("contentsAfter");
                            if (!String.IsNullOrEmpty(tempText))
                            {
                                _rootContentsAfter = tempText.Equals("true",
                                                                     StringComparison.OrdinalIgnoreCase);
                            }
                        }
                    }
                }
            }

            if (_tocExcludedDocuments == null || _tocExcludedDocuments.Count == 0)
            {
                return;
            }

            if (_rootContentsAfter)
            {
                _tocPositionSelector = XPathExpression.Compile(
                    "//body/div[@id='mainSection']/div[@id='mainBody']/div[@id='namespacesSection']");
            }
            else
            {
                _tocPositionSelector = XPathExpression.Compile(
                    "//body/div[@id='mainSection']/div[@id='mainBody']/h1[@class='heading' and .//include[@item='namespacesTitle']]");
            }

            // For the classes...
            _tocClassHSelector = XPathExpression.Compile(
                "//body/div[@id='mainSection']/div[@id='mainBody']/h1[@class='heading' and .//include[@item='classTypesFilterLabel']]");
            _tocClassDivSelector = XPathExpression.Compile(
                "//body/div[@id='mainSection']/div[@id='mainBody']/div[@id='classSection']");

            // For the structures...
            _tocStructHSelector = XPathExpression.Compile(
                "//body/div[@id='mainSection']/div[@id='mainBody']/h1[@class='heading' and .//include[@item='structureTypesFilterLabel']]");
            _tocStructDivSelector = XPathExpression.Compile(
                "//body/div[@id='mainSection']/div[@id='mainBody']/div[@id='structureSection']");

            // For the interfaces...
            _tocInterfaceHSelector = XPathExpression.Compile(
                "//body/div[@id='mainSection']/div[@id='mainBody']/h1[@class='heading' and .//include[@item='interfaceTypesFilterLabel']]");
            _tocInterfaceDivSelector = XPathExpression.Compile(
                "//body/div[@id='mainSection']/div[@id='mainBody']/div[@id='interfaceSection']");

            // For the delegates...
            _tocDelegateHSelector = XPathExpression.Compile(
                "//body/div[@id='mainSection']/div[@id='mainBody']/h1[@class='heading' and .//include[@item='delegateTypesFilterLabel']]");
            _tocDelegateDivSelector = XPathExpression.Compile(
                "//body/div[@id='mainSection']/div[@id='mainBody']/div[@id='delegateSection']");

            // For the enumerations...
            _tocEnumHSelector = XPathExpression.Compile(
                "//body/div[@id='mainSection']/div[@id='mainBody']/h1[@class='heading' and .//include[@item='enumerationTypesFilterLabel']]");
            _tocEnumDivSelector = XPathExpression.Compile(
                "//body/div[@id='mainSection']/div[@id='mainBody']/div[@id='enumerationSection']");
        }