using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;
namespace PrintXML
{
class Program
{
static void Main(string[] args)
{
//读取标准字段的xml中的英文名和中文名;
XmlDocument xmlDoc = new XmlDocument();
string xmlpath = "E:/xxx1.xml";
xmlDoc.Load(xmlpath);
XmlNode root = xmlDoc.DocumentElement;
XmlNodeList xnlist = root.ChildNodes;
Dictionary<string, string> myDirectory = new Dictionary<string, string>();
foreach (XmlNode node in xnlist)
{
if (node.Attributes["name"] != null)
{
string str1 = node.Attributes["name"].Value;
string str2 = node.Attributes["chineseName"].Value;
myDirectory[str1] = str2;
}
}
//读取异构组件的name;
XmlDocument xml = new XmlDocument();
string path = "F:/任务/xxx2.xml";
xml.Load(path);
XmlNode root2 = xml.DocumentElement;
XmlNodeList xnlist2 = root2.ChildNodes;
foreach (XmlNode itemsNode in xnlist2)
{
// 获取name属性
XmlAttribute nameAttr = itemsNode.Attributes["name"];
if (null == nameAttr)
{
continue;
}
foreach (XmlElement itemNode in itemsNode.OfType<XmlElement>())
{
XmlAttribute itemcName = itemNode.Attributes["cName"];
if(null != itemcName)
{
continue;
}
XmlAttribute itemName = itemNode.Attributes["name"];
if (null == itemName)
{
continue;
}
string ename = itemName.Value;
string cname;
if (myDirectory.TryGetValue(ename, out cname))
{
itemNode.SetAttribute("cName", cname);
}
else
{
Console.WriteLine("name={0}",ename);
}
}
}
xml.Save(path + ".out1");
}
}
}