标签:button_ChooseDir
代码:
button_ChooseDir_Click点击事件
private void button_ChooseDir_Click(object sender, EventArgs e)
{
try
{
if (dialog.ShowDialog() == DialogResult.OK)
{
string foldPath = dialog.SelectedPath;
textBox_FileInfo.Text = foldPath;
DirectoryInfo d = new DirectoryInfo(foldPath);
FileInfo[] files = d.GetFiles("*.xml");
List<string> fileNames = new List<string>();
foreach (FileInfo file in files) {
fileNames.Add(file.FullName);
logger.Info(file.FullName);
}
List<ResultLog> data = new List<ResultLog>();
for (int i = 0; i < fileNames.Count; i++) {
string filePath = fileNames[i];
if (getXMLResultLogList(filePath) != null) {
data.Add(getXMLResultLogList(filePath));
}
}
logger.Info("----data---" + data);
}
}
catch (Exception err)
{
logger.Info("XML 解析错误");
logger.Error(err.Message, err);
}
}
getXMLResultLogList方法:
private ResultLog getXMLResultLogList(string filePath) {
try
{
XmlDocument doc = new XmlDocument();
XmlReaderSettings settings = new XmlReaderSettings();
settings.IgnoreComments = true;
XmlReader reader = XmlReader.Create(filePath, settings);
doc.Load(reader);
XmlNode xn = doc.SelectSingleNode("ROOT");
XmlNodeList xnl = xn.ChildNodes;
ResultLog xmlResultLog = new ResultLog();
foreach (XmlNode xn2 in xnl)
{
String name = xn2.Name;
if ("BARCODE".Equals(name))
{
xmlResultLog.BarCode = xn2.InnerText;
}
if ("TESTDATE".Equals(name))
{
xmlResultLog.TestDate = xn2.InnerText;
}
if ("TASKCODE".Equals(name))
{
xmlResultLog.TaskCode = xn2.InnerText;
}
if ("TSP".Equals(name))
{
xmlResultLog.Tsp = xn2.InnerText;
}
if ("ISOK".Equals(name))
{
xmlResultLog.IsOk = xn2.InnerText;
}
if ("RESULTINFO".Equals(name))
{
xmlResultLog.ResultInfo = xn2.InnerText;
}
}
return xmlResultLog;
}
catch (Exception)
{
return null;
}
}
xml文件格式: