又回到了那个相当无聊的保守项目中!还是做中文化,两千多条资源,两天内由日文翻译成中文。我一直纳闷的时公司有专业翻译,为什么却还非得拿到项目组中来让我们这些50音图都读不全的人“翻译”?是不是怕我们闲着会头痛?呵呵。
不说废话了。其实已经有了一份上个版本的中文资源文件,这次只是增加了一部分新的条目而已。问题是原来的资源文件只有xml版本的,而要把它放到Excel中对应的位置去只能手动,负值,查找,粘贴。两千多条虽然不算太多,但操作起来还是比较费时间的。于是准备写个小的工具自动将xml文件中的资源匹配并填充到Excel文件中。很久没写代码了,几行代码写了我一两个小时。。。
xml文件结构

Code
<data name="GridCommon.ActionButtonConfirmMessage">
<value>删除吗?</value>
</data>
代码

Code
private void button1_Click(object sender, EventArgs e)

{


Dictionary<string, string> dictionary = GetXMLValue(@"E:\OPC\OPCWebAP.zh.xml");

ExcelHelper eh = new ExcelHelper(@"E:\OPC\OPC.xls");
string celKey;
string celValue;
for (int i = 1; i < 2300; i++)

{
celKey = eh.ReadCell(i, 1);
if (dictionary.TryGetValue(celKey, out celValue))

{
eh.WriteCell(i, 6, celValue);
}
}
eh.Save(@"E:\OPC\OPC"+ DateTime.Now.ToFileTime() +".xls");
}

private static Dictionary<string, string> GetXMLValue(string xmlPath)

{
XPathNavigator nav;
XPathDocument docNav;
Dictionary<string, string> dictionary = new Dictionary<string, string>();

docNav = new XPathDocument(xmlPath);
nav = docNav.CreateNavigator();
nav.MoveToRoot();

//Move to the first child node (comment field).
nav.MoveToFirstChild();

do

{
//Find the first element.
if (nav.NodeType == XPathNodeType.Element)

{
//Determine whether children exist.
if (nav.HasChildren == true)

{

//Move to the first child.
nav.MoveToFirstChild();

//Loop through all of the children.
do

{
//Display the data.
string value = nav.Value;
nav.MoveToFirstAttribute();
string name = nav.Value;

if (!dictionary.ContainsKey(name))

{
dictionary.Add(name, value);
}

nav.MoveToParent();

} while (nav.MoveToNext());
}
}
} while (nav.MoveToNext());
return dictionary;
}
写的有点乱,记录一下,以后有类似需求时修改下就可以继续使用了。毕竟这个破烂保守项目经常做的就是这种事情