C# 检测两个xml文件的不同并提取,提取完再合并回去

C# 检测不同的xml并提取

提取不同

string appPath = System.Windows.Forms.Application.StartupPath;
        void ExtractXml()
        {
            //线上
            string uiXmlPath1 = appPath + "\\uiExport1.xml";
            //合并
            string uiXmlPath2 = appPath + "\\uiExport2.xml";
            //合并后输出
            string uiXmlPath3 = appPath + "\\uiExport3.xml";
            if (!System.IO.File.Exists(uiXmlPath1) || !System.IO.File.Exists(uiXmlPath2))
            {
                Console.WriteLine("ComPath is null ..........");
                return;
            }
            XmlDocument xml = new XmlDocument();
            xml.Load(uiXmlPath1);
            XmlNode displayList1 = xml.SelectSingleNode("/resources");
            XmlNodeList childlist = displayList1.ChildNodes;

            Dictionary<string, string> addStr = new Dictionary<string, string>();
            foreach (var v in childlist)
            {
                if (v.GetType() == typeof(XmlElement))
                {
                    XmlElement ele = (XmlElement)v;
                    addStr.Add(ele.Attributes["name"].Value + "&" + ele.Attributes["mz"].Value, ele.InnerText);
                }
            }

            XmlDocument xml2 = new XmlDocument();
            xml2.Load(uiXmlPath2);
            XmlNode displayList2 = xml2.SelectSingleNode("/resources");
            XmlNodeList childlist2 = displayList2.ChildNodes;

            Dictionary<string, string> addStr1 = new Dictionary<string, string>();
            foreach (var v in childlist2)
            {
                if (v.GetType() == typeof(XmlElement))
                {
                    XmlElement ele = (XmlElement)v;
                    addStr1.Add(ele.Attributes["name"].Value + "&" + ele.Attributes["mz"].Value, ele.InnerText);
                }
            }

            Dictionary<string, string> extroStr = new Dictionary<string, string>();
            foreach (var v in addStr1)
            {
                if (!addStr.ContainsKey(v.Key))
                {
                    extroStr.Add(v.Key,v.Value);
                }
            }
            XmlDocument xml3 = new XmlDocument();
            xml3.Load(uiXmlPath3);
            XmlNode displayList = xml3.SelectSingleNode("/resources");

            foreach (var item in extroStr)
            {
                XmlElement newNode = (XmlElement)xml3.CreateNode(XmlNodeType.Element, "string", "");
                string[] str = item.Key.Split('&');
               
                newNode.SetAttribute("name", str[0]);
                newNode.SetAttribute("mz", str[1]);
                newNode.InnerText = item.Value;
                displayList.AppendChild(newNode);
            }

            xml3.Save(uiXmlPath3);
主要用来解决多语言版本ui上文字的问题(使用fgui,所以导出了xml文件)。把合并后有但是线上没有的写入新的xml里,合并后的xml一开始为一个只有<resources>和</resources>的空文件。

再把xml合并回去

        void MergeXml()
        {
            //合并
            string uiXmlPath2 = appPath + "\\uiExport2.xml";

            //翻译过的
            string uiXmlPath3 = appPath + "\\uiExport4.xml";

            XmlDocument xml2 = new XmlDocument();
            xml2.Load(uiXmlPath2);
            XmlNode displayList2 = xml2.SelectSingleNode("/resources");
            XmlNodeList childlist2 = displayList2.ChildNodes;

            XmlDocument xml3 = new XmlDocument();
            xml3.Load(uiXmlPath3);
            XmlNode displayList3 = xml3.SelectSingleNode("/resources");
            XmlNodeList childlist3 = displayList3.ChildNodes;

            Dictionary<string, string> addStr2 = new Dictionary<string, string>();
            foreach (var v in childlist3)
            {
                if (v.GetType() == typeof(XmlElement))
                {
                    XmlElement ele = (XmlElement)v;
                    addStr2.Add(ele.Attributes["name"].Value + "&" + ele.Attributes["mz"].Value, ele.InnerText);
                }
            }


            foreach (var v in childlist2)
            {
                if (v.GetType() == typeof(XmlElement))
                {
                    XmlElement ele = (XmlElement)v;
                    string key = ele.Attributes["name"].Value + "&" + ele.Attributes["mz"].Value;
                    if (addStr2.ContainsKey(key))
                    {
                        ele.InnerText = addStr2[key];
                    }

                }
            }
            xml2.Save(uiXmlPath2);
翻译完了以后再合并回原来的xml文件即可。
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值