提取不同
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文件即可。