功能:主要用来翻译.NET中Dll对应XML解析文档成中文。
源码:
/// <summary> /// 加载xml /// </summary> /// <param name="xmlPath"></param> private void readyXml(object xmlPath) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load((string)xmlPath); this.Invoke(new MethodInvoker(delegate() { xmltxt.Text = XmlToString(xmlDoc); })); int num = 0; XmlNodeList memberList = xmlDoc.SelectSingleNode("doc/members").ChildNodes; for (int i = 0; i < memberList.Count; i++) { XmlNodeList itemList = memberList[i].ChildNodes; for (int t = 0; t < itemList.Count; t++) { if (itemList[t].Name == "summary" || itemList[t].Name == "param" || itemList[t].Name == "returns" || itemList[t].Name == "value") { if (!itemList[t].InnerText.StartsWith("【已翻译】")) { string itemStr = ""; Dictionary<string, string> itemChildDesc = new Dictionary<string, string>(); getXmlItemText(itemList[t].ChildNodes, ref itemStr, ref itemChildDesc); string resultstr = translate(itemStr.Trim()); foreach (KeyValuePair<string, string> item in itemChildDesc) { resultstr = resultstr.Replace(item.Key, item.Value); } if (!String.IsNullOrEmpty(resultstr)) itemList[t].InnerText = "【已翻译】 " + "【" + resultstr + "】" + " " + itemList[t].InnerText.Trim().Replace(" ", ""); num++; } } } this.Invoke(new MethodInvoker(delegate() { this.prolab.Text = (i + 1).ToString() + "/" + memberList.Count.ToString(); })); if (num % 20 == 0) { xmlDoc.Save((string)xmlPath); this.Invoke(new MethodInvoker(delegate() { xmltxt.Text = XmlToString(xmlDoc); })); } } xmlDoc.Save((string)xmlPath); this.Invoke(new MethodInvoker(delegate() { filebtn.Enabled = true; xmltxt.Text = XmlToString(xmlDoc); })); if (thread != null) thread.Abort(); } private void getXmlItemText(XmlNodeList itemChildNode, ref string text, ref Dictionary<string, string> desc) { for (int j = 0; j < itemChildNode.Count; j++) { if (itemChildNode[j].NodeType == XmlNodeType.Text) { text += itemChildNode[j].InnerText; } else if (itemChildNode[j].NodeType == XmlNodeType.Element && itemChildNode[j].Name == "see") { string vaK = "[" + "apple" + j + "]"; string vaV = itemChildNode[j].Attributes["cref"].Value; vaV = (vaV.StartsWith("T:") || vaV.StartsWith("E:")) ? vaV.Remove(0, 2) : vaV; desc.Add(vaK, vaV); text += vaK; } else if (itemChildNode[j].NodeType == XmlNodeType.Element && itemChildNode[j].Name == "c") { string vaK = "[" + "banana" + j + "]"; string vaV = itemChildNode[j].InnerText; desc.Add(vaK, vaV); text += vaK; } else if (itemChildNode[j].NodeType == XmlNodeType.Element && itemChildNode[j].Name == "para") { if (!itemChildNode[j].InnerText.StartsWith("【已翻译】")) { string itemStr = ""; Dictionary<string, string> itemChildDesc = new Dictionary<string, string>(); getXmlItemText(itemChildNode[j].ChildNodes, ref itemStr, ref itemChildDesc); string resultstr = translate(itemStr.Trim()); foreach (KeyValuePair<string, string> item in itemChildDesc) { resultstr = resultstr.Replace(item.Key, item.Value); } itemChildNode[j].InnerText = "【已翻译】 " + "【" + resultstr + "】" + " " + itemChildNode[j].InnerText.Trim().Replace(" ", ""); } } } }
源码下载地址:XML翻译.zip