我有工作代码来加载 7 个不同的 XML 文件并将相关数据导入使用数据网格视图显示的数据表中。这对每个文件使用一个单独的打开文件对话框,我现在想创建一个自动按钮,它将按部分名称(“ POP TEST ”)加载第一个文件,并使用我使用文件夹选择的目录中的当前代码解析它对话框。然后我需要它使用部分名称(“ FWD SWP ”)循环到下一个文件,并使用该文件类型的指定代码对其进行解析。
到目前为止,这是我尝试过的,但我收到错误 System.ArgumentException:“路径不是合法形式。”
string partialname = "POP TEST";
FolderBrowserDialog fbd = new FolderBrowserDialog();
fbd.Description = "Pick a folder fool";
DirectoryInfo searhdirectory = new DirectoryInfo(fbd.SelectedPath);
FileInfo[] filesInDir = searhdirectory.GetFiles("*" + partialname + "*.*");
foreach (FileInfo foundfile in filesInDir)
{
string fullname = foundfile.FullName;
}
我的最终目标是一次验证这些 xml 文件,不仅是 1 个文件夹,而且一次最多验证 100 个文件夹。
这是我当前与打开文件对话框一起使用的代码......其次是每个文件夹中我的 7 个不同 XML 文件中的每一个的类似代码。我希望使用浏览文件夹结构将打开文件对话框替换为文件加载方法,以选择结果所在的文件夹...
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFileDialog ofdPOPREF = new OpenFileDialog();
ofdPOPREF.Title = "Open POP TEST [REF]";
if (ofdPOPREF.ShowDialog() == DialogResult.OK)
{
var path = ofdPOPREF.FileName;
MessageBox.Show("POP TEST [REF] Successfully Imported");
}
else
{
MessageBox.Show("Cancelled");
return;
}
DataTable dt1 = new DataTable();
dt1.Columns.Add("device", typeof(string));
dt1.Columns.Add("model", typeof(string));
dt1.Columns.Add("serial number", typeof(string));
dt1.Columns.Add("cal site", typeof(string));
dt1.Columns.Add("cal date", typeof(DateTime));
dt1.Columns.Add("unit", typeof(string));
dt1.Columns.Add("temperature", typeof(int));
dt1.Columns.Add("fw version", typeof(string));
dt1.Columns.Add("system", typeof(string));
dt1.Columns.Add("channel", typeof(string));
dt1.Columns.Add("test plan", typeof(string));
dt1.Columns.Add("user", typeof(string));
dt1.Columns.Add("site", typeof(string));
dt1.Columns.Add("site address", typeof(string));
dt1.Columns.Add("work order", typeof(string));
dt1.Columns.Add("guid", typeof(string));
dt1.Columns.Add("comment", typeof(string));
dt1.Columns.Add("test date", typeof(DateTime));
dt1.Columns.Add("test start time", typeof(DateTime));
dt1.Columns.Add("test series", typeof(string));
dt1.Columns.Add("status", typeof(int));
XDocument docpopref = XDocument.Load(ofdPOPREF.FileName);
XElement root = docpopref.Root;
XElement header = root.Element("Header");
if (header != null)
{
string device = (string)header.Element("device");
string model = (string)header.Element("model");
string sn = (string)header.Element("serialNumber");
string calSite = (string)header.Element("calSite");
DateTime calDate = (DateTime)header.Element("calDate");
string unit = (string)header.Element("temperature").Element("unit");
int temperature = (int)header.Element("temperature").Element("value");
string fwVersion = (string)header.Element("fwVersion");
string system = (string)header.Element("system");
string channel = (string)header.Element("channel");
string testPlan = (string)header.Element("testPlan");
string user = (string)header.Element("user");
string site = (string)header.Element("site");
string siteAddr = (string)header.Element("siteAddr");
string workOrder = (string)header.Element("workOrder");
string guid = (string)header.Element("guid");
string comment = (string)header.Element("comment");
DateTime testDate = (DateTime)header.Element("testDate");
TimeSpan testTime = DateTime.ParseExact((string)header.Element("testTime"), "HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture).TimeOfDay;
testDate = testDate.Add(testTime);
DateTime testStartTime = (DateTime)header.Element("testStartTime");
string testSeries = (string)header.Element("testSeries");
int status = (int)header.Element("status");
dt1.Rows.Add(new object[] {
device, model, sn, calSite, calDate, unit, temperature, fwVersion, system,
channel, testPlan, user, site, siteAddr, workOrder, guid, comment, testDate, testStartTime, testSeries, status
});
dataGridView1.DataSource = dt1;
}
XElement testData = root.Element("TestData");
string xType = (string)testData.Element("type");
DataTable dt2 = new DataTable();
XElement data = testData.Descendants("Data").FirstOrDefault();
switch (xType)
{
case "AutoTest":
dt2.Columns.Add("Type", typeof(string));
dt2.Columns.Add("cID", typeof(int));
dt2.Columns.Add("mID", typeof(int));
dt2.Columns.Add("lID", typeof(int));
dt2.Columns.Add("Unit", typeof(int));
dt2.Columns.Add("Sto", typeof(int));
dt2.Columns.Add("Val", typeof(decimal));
foreach (XElement ch in data.Elements("CH"))
{
int cID = (int)ch.Attribute("cID");
foreach (XElement meas in ch.Descendants("Meas"))
{
int mID = (int)meas.Attribute("mID");
int lID = (int)meas.Attribute("lID");
int dataUnit = (int)meas.Attribute("Unit");
int sto = (int)meas.Attribute("Sto");
decimal? val = (string)meas.Attribute("Val") == string.Empty ? null : (decimal?)meas.Attribute("Val");
dt2.Rows.Add(new object[] { "Meas", cID, mID, lID, dataUnit, sto, val });
}
XElement subData = ch.Descendants("Data").FirstOrDefault();
if (subData != null)
{
foreach (XElement mList in subData.Descendants("mList"))
{
int mID = (int)mList.Attribute("mID");
int lID = (int)mList.Attribute("lID");
int dataUnit = (int)mList.Attribute("Unit");
int sto = (int)mList.Attribute("Sto");
string val = (string)mList.Attribute("Val");
decimal? numVal = null;
switch (val)
{
case "True":
numVal = 1;
break;
case "False":
numVal = 0;
break;
default:
numVal = val == null ? null : val == string.Empty ? null : (decimal?)decimal.Parse(val);
break;
}
dt2.Rows.Add(new object[] { "Mlist", cID, mID, lID, dataUnit, sto, numVal });
}
}
}
break;
}
dataGridView2.DataSource = dt2;
}