/// <summary>
        /// 点击选择按钮事件
        /// </summary>
        /// <param name="sender">响应对象</param>
        /// <param name="e">响应参数</param>
        private void Button_Select_Click(object sender, RoutedEventArgs e)
        {
            switch (ControlType)
            {
            case EnumControlType.LoadFile:
                using (WH_CommonFunc.OpenFileDialogGetOpenFile(TextBox_Path, Filter, TitleDescription, DefaultDirectory)) { };
                break;

            case EnumControlType.SaveFile:
                using (WH_CommonFunc.OpenFileDialogGetSavePath(TextBox_Path, Filter, TitleDescription, DefaultDirectory)) { };
                break;

            case EnumControlType.SelectPath:
                using (WH_CommonFunc.OpenDirectoryDialogGetFolder(TextBox_Path, TitleDescription, DefaultDirectory)) { };
                break;

            default:
                break;
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 生成合并数据
        /// </summary>
        public void GenerateCombineData()
        {
            List <XDocument> docs = new List <XDocument>();
            Dictionary <string, XElement>         dictElement = new Dictionary <string, XElement>();
            Dictionary <string, List <XComment> > dictComment = new Dictionary <string, List <XComment> >();
            XElement        existElement;
            XAttribute      existAttribut;
            List <XComment> existComments;
            XComment        tempComment;
            FileInfo        file = null;

#if !DEBUG
            try
#endif
            {
                foreach (SC2_FileListViewItem item in ListView_FileList.Items)
                {
                    file = item.SelectPathControl_FilePath.SelectedFile;
                    XDocument doc = XDocument.Load(file.FullName);
                    docs.Add(doc);
                }
            }
#if !DEBUG
            catch (Exception error)
            {
                string msg = file == null ? "" : $"错误文件:{file.FullName}\r\n";
                msg += error.Message;
                MessageBox.Show(msg);
            }
#endif
            XDocument result = new XDocument
            {
                Declaration = docs[0].Declaration
            };
            XElement resultRoot = new XElement(Const_XMLRootName);
            result.Add(resultRoot);

            foreach (XDocument xml in docs)
            {
                XElement root = xml.Element(Const_XMLRootName);
                if (root == null)
                {
                    continue;
                }

                // 数据实体
                foreach (XElement element in root.Elements())
                {
                    string id = element.Attribute(Const_XMLIDName)?.Value;
                    if (string.IsNullOrEmpty(id))
                    {
                        continue;
                    }
                    if (dictElement.ContainsKey(id))
                    {
                        existElement = dictElement[id];
                        existElement.Add(element.Elements());

                        // 数据属性
                        foreach (XAttribute attribute in element.Attributes())
                        {
                            existAttribut = existElement.Attribute(attribute.Name);
                            if (existAttribut == null)
                            {
                                existElement.Add(attribute);
                            }
                            else
                            {
                                existAttribut.Value = attribute.Value;
                            }
                        }
                    }
                    else
                    {
                        existElement    = new XElement(element);
                        dictElement[id] = existElement;
                        resultRoot.Add(existElement);
                    }

                    // 注释
                    List <XComment> comments = new List <XComment>();
                    XNode           node     = element.PreviousNode;
                    while (node != null && node is XComment comment)
                    {
                        comments.Insert(0, comment);
                        node = node.PreviousNode;
                    }
                    if (comments.Count != 0)
                    {
                        if (dictComment.ContainsKey(id))
                        {
                            existComments = dictComment[id];
                            foreach (XComment comment in comments)
                            {
                                if (existComments.Where(r => r.Value == comment.Value).Count() == 0)
                                {
                                    tempComment = new XComment(comment);
                                    existElement.AddBeforeSelf(tempComment);
                                    existComments.Add(tempComment);
                                }
                            }
                        }
                        else
                        {
                            dictComment[id] = new List <XComment>();
                            foreach (XComment comment in comments)
                            {
                                tempComment = new XComment(comment);
                                existElement.AddBeforeSelf(tempComment);
                                dictComment[id].Add(tempComment);
                            }
                        }
                    }
                }
            }

            if (WH_CommonFunc.SaveFilePathDialog(file.Directory.FullName, "XML|*.xml", "结果文件保存到", out System.Windows.Forms.SaveFileDialog saveFileDialog) == System.Windows.Forms.DialogResult.OK)
            {
                result.Save(saveFileDialog.FileName);
            }
        }