private void OpenXmlFile(bool autoRenameWhenNamingConflict = false) { try { using (var xmlToCsvUsingDataSet = new XmlToCsvUsingDataSet(FileName, autoRenameWhenNamingConflict)) { _xmlToCsvContext = new XmlToCsvContext(xmlToCsvUsingDataSet); } txbLog.Text += @"-Opening file '" + FileName + @"' completed." + Environment.NewLine; CreateTreeview(); } catch (DuplicateNameException ex) { txbLog.Text += @"-DuplicateName Execption: " + ex.Message + Environment.NewLine; const string messageBoxText = @"The XML data contains conflicting names between table and column names. Do you want to continue by renaming the conflicting elements in the resulting CSV?"; const string caption = @"Duplicate Name Conflict"; var result = MessageBox.Show(messageBoxText, caption, MessageBoxButton.YesNo, MessageBoxImage.Warning); if (result == MessageBoxResult.Yes) { OpenXmlFile(true); txbLog.Text += @"-DuplicateName Execption resolved by auto-renaming conflicting table name: " + Environment.NewLine; } } catch (XmlException) { const string messageBoxText = "-Invalid XML file. Please make sure the XML file is XML-compliant."; const string caption = "Invalid XML error"; txbLog.Text = messageBoxText + " Opening file '" + FileName + "' did not complete." + Environment.NewLine; MessageBox.Show(txbLog.Text, caption, MessageBoxButton.OK, MessageBoxImage.Error); } catch (ArgumentException ex) { const string messageBoxText = "-An argument provided is invalid. The error message: "; const string caption = "Argument Exception"; txbLog.Text = messageBoxText + " Argument Execption: " + ex.Message + Environment.NewLine; MessageBox.Show(txbLog.Text, caption, MessageBoxButton.OK, MessageBoxImage.Error); } catch (InvalidOperationException ex) { string messageBoxText = "-Invalid operation. The error message: " + ex.Message; const string caption = "Invalid operation"; txbLog.Text = messageBoxText + Environment.NewLine; MessageBox.Show(txbLog.Text, caption, MessageBoxButton.OK, MessageBoxImage.Error); } }
/// <summary> /// XML to CSV Command Line Converter. User as follows: XmlToCsv.Console.exe -xml C:\payslip.xml -dir C:\my_output /// </summary> /// <param name="args">First parameter selects the XML file, second parameter specifies the output directory.</param> static void Main(string[] args) { //Command line parsing library from http://cmdline.codeplex.com/ var cmd = new ConsoleCmdLine(); var xmlInputFilePath = new CmdLineString("xml", true, "XML file to convert."); var outputDirectory = new CmdLineString("dir", true, "Directory to save the CSV result to."); cmd.RegisterParameter(xmlInputFilePath); cmd.RegisterParameter(outputDirectory); cmd.Parse(args); var converter = new XmlToCsvUsingDataSet(xmlInputFilePath.Value); var context = new XmlToCsvContext(converter); foreach (string xmlTableName in context.Strategy.TableNameCollection) { context.Execute(xmlTableName, outputDirectory.Value + @"\" + xmlTableName + ".csv", Encoding.Unicode); } }
private void OpenXmlFile(CancelEventArgs e, bool autoRenameWhenNamingConflict = false) { try { using (var xmlToCsvUsingDataSet = new XmlToCsvUsingDataSet(openFileDialog1.FileName, autoRenameWhenNamingConflict)) { _xmlToCsvContext = new XmlToCsvContext(xmlToCsvUsingDataSet); } lblStatus.Text = openFileDialog1.FileName; txbLog.Text += @"Opening file '" + openFileDialog1.FileName + @"' completed." + Environment.NewLine; foreach (string item in _xmlToCsvContext.Strategy.TableNameCollection) { lsbTables.Items.Add(item); } butSelectFolder.Enabled = lsbTables.Items.Count > 0; } catch (DuplicateNameException ex) { DialogResult result = MessageBox.Show(@"The XML data contains conflicting names between table and column names. Do you want to continue by renaming the conflicting elements in the resulting CSV?", @"Duplicate Name Conflict", MessageBoxButtons.YesNo, MessageBoxIcon.Error); if (result == DialogResult.Yes) { //e.Cancel = true; OpenXmlFile(e, true); } txbLog.Text += @"DuplicateName Execption: " + ex.Message + Environment.NewLine; } catch (XmlException) { DialogResult result = MessageBox.Show(this, @"Invalid XML file. Please make sure the XML file is XML-compliant.", @"Invalid XML error", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error); txbLog.Text += @"Invalid XML file. Please make sure the XML file is XML-compliant. Opening file '" + openFileDialog1.FileName + @"' did not complete." + Environment.NewLine; if (result == DialogResult.Retry) { e.Cancel = true; } } catch (ArgumentException ex) { DialogResult result = MessageBox.Show(this, @"An argument provided is invalid. The error message: " + ex.Message, @"Argument Exception", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error); txbLog.Text += @"Argument Execption: " + ex.Message + Environment.NewLine; if (result == DialogResult.Retry) { e.Cancel = true; } } catch (InvalidOperationException ex) { DialogResult result = MessageBox.Show(this, @"Invalid operation. The error message: " + ex.Message, @"Invalid operation", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error); txbLog.Text += @"Invalid Operation Execption: " + ex.Message + Environment.NewLine; if (result == DialogResult.Retry) { e.Cancel = true; } } catch (Exception ex) { DialogResult result = MessageBox.Show(this, @"Unexpected error.", @"Unexpected error", MessageBoxButtons.OK, MessageBoxIcon.Error); txbLog.Text += @"Unexpected error: " + ex.Message + Environment.NewLine; } }