Click here to Skip to main content
15,887,135 members
Home / Discussions / C#
   

C#

 
AnswerRe: How to find interfaces in code programmatically Pin
jschell28-May-14 8:46
jschell28-May-14 8:46 
AnswerRe: How to find interfaces in code programmatically Pin
BobJanova29-May-14 1:20
BobJanova29-May-14 1:20 
QuestionLoading and unloading assemblies using reflection Pin
nitin_ion28-May-14 1:38
nitin_ion28-May-14 1:38 
SuggestionRe: Loading and unloading assemblies using reflection Pin
Richard MacCutchan28-May-14 2:46
mveRichard MacCutchan28-May-14 2:46 
AnswerRe: Loading and unloading assemblies using reflection Pin
Rob Philpott28-May-14 4:52
Rob Philpott28-May-14 4:52 
QuestionInserting missing attributes in XML / app.config file Pin
Flockston8627-May-14 22:59
Flockston8627-May-14 22:59 
AnswerRe: Inserting missing attributes in XML / app.config file Pin
Dave Kreskowiak28-May-14 2:29
mveDave Kreskowiak28-May-14 2:29 
AnswerRe: Inserting missing attributes in XML / app.config file Pin
Flockston863-Jun-14 1:20
Flockston863-Jun-14 1:20 
Hi,

just wanted you to inform that I have a solution now. I had a closer look into the XML part of .net and came up with the following solution that works pretty well:

C#
private bool syncConfigFiles(string serverConfigFile, string localConfigFile)
        {
            XmlDocument _localDoc = new XmlDocument();
            XmlDocument _serverDoc = new XmlDocument();

            //Load the two config files for comparison
            _localDoc.Load(localConfigFile);
            _serverDoc.Load(serverConfigFile);

            //Get the various child nodes of the documents
            XmlNodeList _localNodes = _localDoc.DocumentElement.ChildNodes;
            XmlNodeList _serverNodes = _serverDoc.DocumentElement.ChildNodes;

            //Loop through all nodes of the server config file
            foreach (XmlNode _serverNode in _serverNodes)
            {
                //Loop through all nodes of the local config file
                foreach (XmlNode _localNode in _localNodes)
                {
                    if (_serverNode.Name == _localNode.Name)
                    {
                        //Get all the attributes of the current node of the server file
                        XmlAttributeCollection _serverAttributes = _serverNode.Attributes;

                        //Check whether attributes exist, like that, only the tester family parts 
                        if (_serverAttributes != null)
                        {
                            for (int i = 0; i < _serverAttributes.Count; i++)
                            {
                                //Check whether the current server attribute exists within local attributes list or not (= equals null)
                                if (_localNode.Attributes[_serverAttributes[i].Name] == null)
                                {
                                    //Create new node for import of the attributes (otherwise exception as it is not part of the current document)
                                    XmlNode _newNode = _localNode.OwnerDocument.ImportNode(_serverNode, true);
                                    if (i > 0)
                                    {
                                        //if it is not the first attribute, insert it after the last checked and present attribute
                                        _localNode.Attributes.InsertAfter(_newNode.Attributes[_serverAttributes[i].Name], _localNode.Attributes[_serverAttributes[i - 1].Name]);
                                    }
                                    else
                                    {
                                        //if it is the first attribute, insert it before the next present attribute
                                        int _tempI = i;
                                        while (_localNode.Attributes[_serverAttributes[_tempI + 1].Name] == null && _tempI < _serverAttributes.Count)
                                        {
                                            _tempI++;
                                        }
                                        //needed if only one element is present
                                        if (_tempI < _serverAttributes.Count)
                                        {
                                            _localNode.Attributes.InsertBefore(_newNode.Attributes[_serverAttributes[i].Name], _localNode.Attributes[_serverAttributes[_tempI + 1].Name]);
                                        }
                                        else
                                        {
                                            _localNode.Attributes.Append(_newNode.Attributes[_serverAttributes[i].Name]);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            /*
             * Declare XmlWriterSettings:
             * - each attribute gets a new line
             * - indent is needed to activate the previous setting
             * - CloseOutput frees the underlying stream writer, so that the file is not locked after writing
             */

            XmlWriterSettings _settings = new XmlWriterSettings();
            _settings.NewLineOnAttributes = true;
            _settings.Indent = true;
            _settings.CloseOutput = true;

            //Create file and close file handling
            XmlWriter _writer = XmlWriter.Create(localConfigFile, _settings);
            _localDoc.Save(_writer);
            _writer.Close();
            return true;
        }


Log entries and some checks are still missing, but the functionality is given.

Best regards
Flockston
GeneralRe: Inserting missing attributes in XML / app.config file Pin
Dave Kreskowiak3-Jun-14 2:14
mveDave Kreskowiak3-Jun-14 2:14 
GeneralRe: Inserting missing attributes in XML / app.config file Pin
Flockston863-Jun-14 2:25
Flockston863-Jun-14 2:25 
GeneralRe: Inserting missing attributes in XML / app.config file Pin
Dave Kreskowiak3-Jun-14 4:42
mveDave Kreskowiak3-Jun-14 4:42 
QuestionVisual Studio Output to Run as Admin always Pin
Subin Mavunkal27-May-14 22:57
Subin Mavunkal27-May-14 22:57 
AnswerRe: Visual Studio Output to Run as Admin always Pin
Richard MacCutchan28-May-14 0:53
mveRichard MacCutchan28-May-14 0:53 
Questioncustom file properties Pin
jeffingeorge27-May-14 22:38
jeffingeorge27-May-14 22:38 
AnswerRe: custom file properties Pin
Chris Quinn27-May-14 22:48
Chris Quinn27-May-14 22:48 
AnswerRe: custom file properties Pin
Eddy Vluggen28-May-14 0:28
professionalEddy Vluggen28-May-14 0:28 
QuestionFile opertions Pin
jeffingeorge27-May-14 22:36
jeffingeorge27-May-14 22:36 
AnswerRe: File opertions Pin
Karen Mitchelle27-May-14 22:51
professionalKaren Mitchelle27-May-14 22:51 
AnswerRe: File opertions Pin
Eddy Vluggen28-May-14 0:28
professionalEddy Vluggen28-May-14 0:28 
Questionconvert string to system.linq.iqueryable in C# Pin
joji joseph27-May-14 22:08
joji joseph27-May-14 22:08 
AnswerRe: convert string to system.linq.iqueryable in C# Pin
Chris Quinn27-May-14 22:48
Chris Quinn27-May-14 22:48 
AnswerRe: convert string to system.linq.iqueryable in C# Pin
Dave Kreskowiak28-May-14 2:18
mveDave Kreskowiak28-May-14 2:18 
QuestionClone a printer & change default settings using C# Pin
Dazed.dnc27-May-14 19:03
Dazed.dnc27-May-14 19:03 
QuestionRe: Clone a printer & change default settings using C# Pin
Richard MacCutchan27-May-14 21:18
mveRichard MacCutchan27-May-14 21:18 
AnswerRe: Clone a printer & change default settings using C# Pin
Dazed.dnc28-May-14 13:02
Dazed.dnc28-May-14 13:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.