0

I have working code to load 7 different XML files and import the relevant data into data tables displaying using data grid views. This uses an individual open file dialog per file, I would now like to create an automated button which will load the first file by partial name ("POP TEST") and parse it using my current code from a directory which I choose using a folder dialog box. I then need it to loop through to the next file using partial name ("FWD SWP") and parse it using the specified code for that file type.

This is what I have tried so far but i get an error System.ArgumentException: 'The path is not of a legal form.'

        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;
        }

My end goal is to validate these xml files not only 1 folder at a time but up to 100 folders at a time.

This is my current code in use with the open file dialog... Followed by similar code for each of my 7 different XML files in each folder. I am hoping to replace the Open File Dialog with the File load method using a browse folder structure to select which folder the results are in...

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;
        }
Steve Carr
  • 13
  • 3
  • The default path is not set. Try something like this : fbd.SelectedPath = @"c:\temp"; – jdweng Apr 16 '19 at 09:13
  • But wouldn't fbd.SelectedPath be the default path as it needs to be changeable depending on where the files are saved. – Steve Carr Apr 16 '19 at 22:14
  • Once you select a folder in the dialog it will be updated. But it needs to be valid before you use the method SelectedPath. – jdweng Apr 16 '19 at 23:55

0 Answers0