コード例 #1
0
        public void UpdateXmlFiles(List <FileInfo> filesByNameInDirectory, List <ModInfoContainer> lstModInfos)
        {
            foreach (FileInfo currentFileInfo in filesByNameInDirectory)
            {
                ModInfoContainer foundModInfo = lstModInfos.FirstOrDefault(x => x.Name.ToLowerInvariant() == currentFileInfo.Directory.Name.ToLowerInvariant());

                if (foundModInfo == null)
                {
                    continue;
                }

                //get file
                XDocument xdoc = XDocument.Load(currentFileInfo.FullName);

                bool changed = false;
                //for each id in excel
                //Values(ID)(LANGUAGE)
                foreach (var currentID in foundModInfo.Values)
                {
                    //for each language
                    foreach (var currentLanguage in currentID.Value)
                    {
                        if (UpdateOrInsertValue(xdoc, currentID.Key, currentLanguage.Key, currentLanguage.Value))
                        {
                            changed = true;
                        }
                    }
                }


                // Now check if someone deleted a row
                IEnumerable <XElement> keysInXml = xdoc.Descendants().Where(x => x.Name == KEY_NAME);
                foreach (XElement currentKeyElement in keysInXml.ToList())
                {
                    string currentKeyId = currentKeyElement.Attribute(ID_NAME).Value;
                    if (foundModInfo.Values.Keys.Contains(currentKeyId))
                    {
                        // hmm, forgot what this was for
                        var a = true;
                    }
                    else
                    {
                        currentKeyElement.Remove();
                        changed = true;
                    }
                }

                if (changed)
                {
                    // remove comment due to public request

                    //XComment comment = (from node in xdoc.Nodes() where node.NodeType == XmlNodeType.Comment select node as XComment).FirstOrDefault();

                    //string commentText = String.Format(" Edited with tabler. ");

                    //if (comment == null) {
                    //    xdoc.AddFirst(new XComment(commentText));
                    //} else {
                    //    comment.Value = commentText;
                    //}

                    var xmlSettings = new XmlWriterSettings {
                        Indent      = true,
                        IndentChars = "    "
                    };

                    var configHelper = new ConfigHelper();
                    var settings     = configHelper.GetSettings();

                    if (settings != null)
                    {
                        if (settings.IndentationSettings == IndentationSettings.Spaces)
                        {
                            var indentChars = "";

                            for (int i = 0; i < settings.TabSize; i++)
                            {
                                indentChars += " ";
                            }

                            xmlSettings.IndentChars = indentChars;
                        }
                        if (settings.IndentationSettings == IndentationSettings.Tabs)
                        {
                            xmlSettings.IndentChars = "\t";
                        }


                        if (settings.RemoveEmptyNodes)
                        {
                            xdoc.Descendants().Where(d => d.IsEmpty || String.IsNullOrWhiteSpace(d.Value)).Remove();
                        }
                    }


                    using (var writer = XmlWriter.Create(currentFileInfo.FullName, xmlSettings)) {
                        xdoc.Save(writer);
                    }
                }
            }
        }