Smrf.NodeXL.ExcelTemplate.NodeXLApplicationSettingsBase.CopyAndFilterSettings C# (CSharp) Method

CopyAndFilterSettings() protected method

protected CopyAndFilterSettings ( String sSourceSettings, String sDestinationSettings ) : String
sSourceSettings String
sDestinationSettings String
return String
    CopyAndFilterSettings
    (
        String sSourceSettings,
        String sDestinationSettings
    )
    {
        Debug.Assert( !String.IsNullOrEmpty(sSourceSettings) );
        Debug.Assert( !String.IsNullOrEmpty(sDestinationSettings) );
        AssertValid();

        // We don't want to modify the user's notification, dialog position,
        // or password settings that are stored in the destination.  Use
        // XmlDocuments to copy only the other settings from the source to the
        // destination.

        XmlDocument oSourceDocument = new XmlDocument();
        oSourceDocument.LoadXml(sSourceSettings);

        XmlDocument oDestinationDocument = new XmlDocument();
        oDestinationDocument.LoadXml(sDestinationSettings);

        // See the DefaultStandardSettingsFileContents constant for an example
        // of what the XML documents look like.

        const String SectionGroupXPath =
            "configuration/configSections/sectionGroup";

        const String UserSettingsXPath = "configuration/userSettings";

        XmlNode oSourceSectionGroupNode =
            XmlUtil2.SelectRequiredSingleNode(oSourceDocument,
                SectionGroupXPath, null);

        XmlNode oDestinationSectionGroupNode =
            XmlUtil2.SelectRequiredSingleNode(oDestinationDocument,
                SectionGroupXPath, null);

        XmlNode oSourceUserSettingsNode =
            XmlUtil2.SelectRequiredSingleNode(oSourceDocument,
                UserSettingsXPath, null);

        XmlNode oDestinationUserSettingsNode =
            XmlUtil2.SelectRequiredSingleNode(oDestinationDocument,
                UserSettingsXPath, null);

        foreach (XmlNode oSourceUserSettingsChildNode in
            oSourceUserSettingsNode.ChildNodes)
        {
            // Work item:
            //
            // Figure out a more robust scheme that doesn't use hard-coded
            // class names.

            String sChildName = oSourceUserSettingsChildNode.Name;

            if (
                sChildName.IndexOf("Dialog") >= 0
                ||
                sChildName.IndexOf("NotificationUserSettings") >= 0
                ||
                sChildName.IndexOf("PasswordUserSettings") >= 0
                )
            {
                continue;
            }

            // This user settings child node needs to be copied from the source
            // to the destination.

            XmlNode oSourceUserSettingsChildNodeClone =
                oDestinationDocument.ImportNode(
                    oSourceUserSettingsChildNode, true);

            // Does the node already exist in the destination?

            XmlNode oDestinationUserSettingsChildNode =
                oDestinationUserSettingsNode.SelectSingleNode(sChildName);

            if (oDestinationUserSettingsChildNode == null)
            {
                // No.  Append the node and its corresponding section child
                // node to the destination.

                oDestinationUserSettingsNode.AppendChild(
                    oSourceUserSettingsChildNodeClone);

                XmlNode oSourceSectionNode =
                    XmlUtil2.SelectRequiredSingleNode(
                        oSourceSectionGroupNode,
                        "section[@name=\"" + sChildName + "\"]",
                        null);

                oDestinationSectionGroupNode.AppendChild(
                    oDestinationDocument.ImportNode(
                        oSourceSectionNode, true) );
            }
            else
            {
                // Yes.  Replace the destination node.

                oDestinationUserSettingsNode.ReplaceChild(
                    oSourceUserSettingsChildNodeClone,
                    oDestinationUserSettingsChildNode);
            }
        }

        // Write the edited copy to a string in such a way that it includes a
        // UTF-8 encoding attribute.

        oSourceDocument = null;
        MemoryStream oMemoryStream = new MemoryStream();
        oDestinationDocument.Save(oMemoryStream);
        oDestinationDocument = null;

        return ( Encoding.UTF8.GetString( oMemoryStream.ToArray() ) );
    }