private static XDocument MergeActiveProfile(XDocument defaultDocument, XElement activeProfileElement)
        {
            XElement root = defaultDocument.Root;

            foreach (XElement current in activeProfileElement.Elements())
            {
                XElement xElement = root.Descendants(current.Name).FirstOrDefault <XElement>();
                if (xElement == null)
                {
                    root.Add(current);
                }
                else
                {
                    xElement.Remove();
                    root.Add(AppInsightsConfigReader.MergeElements(xElement, current));
                }
            }
            return(defaultDocument);
        }
        private static XElement MergeElements(XElement existingElement, XElement activeElement)
        {
            XElement xElement = new XElement(activeElement);

            foreach (XElement current in existingElement.Elements())
            {
                XElement xElement2 = xElement.Descendants(current.Name).FirstOrDefault <XElement>();
                if (xElement2 == null)
                {
                    xElement.Add(current);
                }
                else
                {
                    if (current.HasElements)
                    {
                        xElement2.Remove();
                        xElement.Add(AppInsightsConfigReader.MergeElements(current, xElement2));
                    }
                }
            }
            return(xElement);
        }
        /// <summary>
        /// Gets the configuration for the corresponding configuration section.
        /// </summary>
        public string GetConfiguration(ConfigurationSections configSection)
        {
            XDocument xDocument = null;

            if (this.Configuration != null)
            {
                XElement root          = this.Configuration.Root;
                XElement xElement      = root.Element(AppInsightsConfigReader.AINamespace + "ActiveProfile");
                XElement xElement2     = root.Element(AppInsightsConfigReader.AINamespace + "Profiles");
                string   activeProfile = (xElement != null) ? xElement.Value : null;
                if (xElement2 != null && activeProfile != null)
                {
                    XElement xElement3 = xElement2.Element(AppInsightsConfigReader.AINamespace + "Defaults");
                    xElement = (
                        from e in xElement2.Elements(AppInsightsConfigReader.AINamespace + "Profile")
                        where e.Attribute("name") != null && string.Equals(e.Attribute("name").Value, activeProfile)
                        select e).FirstOrDefault <XElement>();
                    if (xElement != null)
                    {
                        xDocument = new XDocument(new object[]
                        {
                            new XElement(AppInsightsConfigReader.AINamespace + "ProfileSettings", (xElement3 != null) ? xElement3.Elements() : null)
                        });
                        xDocument = AppInsightsConfigReader.MergeActiveProfile(xDocument, xElement);
                        if (!AppInsightsConfigReader.IsFlagSet(configSection, ConfigurationSections.ServerAnalytics))
                        {
                            XElement xElement4 = xDocument.Descendants(AppInsightsConfigReader.AINamespace + "ServerAnalytics").FirstOrDefault <XElement>();
                            if (xElement4 != null)
                            {
                                xElement4.Remove();
                            }
                        }
                        if (!AppInsightsConfigReader.IsFlagSet(configSection, ConfigurationSections.ClientAnalytics))
                        {
                            XElement xElement5 = xDocument.Descendants(AppInsightsConfigReader.AINamespace + "ClientAnalytics").FirstOrDefault <XElement>();
                            if (xElement5 != null)
                            {
                                xElement5.Remove();
                            }
                        }
                        if (!AppInsightsConfigReader.IsFlagSet(configSection, ConfigurationSections.MMAOutputChannels))
                        {
                            XElement xElement6 = xDocument.Descendants(AppInsightsConfigReader.AINamespace + "MMAOutputChannels").FirstOrDefault <XElement>();
                            if (xElement6 != null)
                            {
                                xElement6.Remove();
                            }
                        }
                        if (!AppInsightsConfigReader.IsFlagSet(configSection, ConfigurationSections.APMSettings))
                        {
                            XElement xElement7 = xDocument.Descendants(AppInsightsConfigReader.AINamespace + "APMSettings").FirstOrDefault <XElement>();
                            if (xElement7 != null)
                            {
                                xElement7.Remove();
                            }
                        }
                        if (!AppInsightsConfigReader.IsFlagSet(configSection, ConfigurationSections.MemoryEventSettings))
                        {
                            XElement xElement8 = xDocument.Descendants(AppInsightsConfigReader.AINamespace + "MemoryEventSettings").FirstOrDefault <XElement>();
                            if (xElement8 != null)
                            {
                                xElement8.Remove();
                            }
                        }
                        if (!AppInsightsConfigReader.IsFlagSet(configSection, ConfigurationSections.PerformanceCounters))
                        {
                            XElement xElement9 = xDocument.Descendants(AppInsightsConfigReader.AINamespace + "PerformanceCounters").FirstOrDefault <XElement>();
                            if (xElement9 != null)
                            {
                                xElement9.Remove();
                            }
                        }
                    }
                }
            }
            if (xDocument == null)
            {
                return(null);
            }
            return(xDocument.ToString());
        }