public void DataSetImplementationTest() { const string path = @"TestData/data.xml"; var context = new XmlToCsvContext(new XmlToCsvUsingDataSet(path)); Assert.That(context.Strategy.TableNameCollection.Count, Is.EqualTo(1)); foreach (var xmlTableName in context.Strategy.TableNameCollection) { context.Execute(xmlTableName, @"dataDataSet_" + xmlTableName + ".csv", Encoding.Default); } }
public void DuplicateNameRenamedTest() { const string path = @"TestData/ErrorDuplicateName.xml"; var context = new XmlToCsvContext(new XmlToCsvUsingDataSet(path, true)); Assert.AreEqual(2, context.Strategy.TableNameCollection.Count); foreach (var xmlTableName in context.Strategy.TableNameCollection) { context.Execute(xmlTableName, @"ErrorDuplicateName_" + xmlTableName + ".csv", Encoding.Default); } }
public void DoubleQuoteEscapeTest() { const string path = @"TestData/DoubleQuoteEscape.xml"; var context = new XmlToCsvContext(new XmlToCsvUsingDataSet(path)); Assert.That(context.Strategy.TableNameCollection.Count, Is.EqualTo(1)); foreach (var xmlTableName in context.Strategy.TableNameCollection) { context.Execute(xmlTableName, string.Format(@"TestData/dataDataSet_{0}.csv", xmlTableName), Encoding.Default); } }
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); } }
public List <QvxTable> Init(string server, string auth, string username, string password, string param, List <QvxTable> MTables, Func <string, IEnumerable <QvxTable>, QvxTable> FindTable) { QvxLog.Log(QvxLogFacility.Application, QvxLogSeverity.Debug, "Init()"); Dictionary <string, string> parameters = null; MyWebRequest q = prepareRequest(server, auth, username, password, param, out parameters); string s = q.GetResponse(); List <QvxTable> lt = new List <QvxTable>(); if (q.StatusCode == HttpStatusCode.OK) { XmlDocument doc = JsonConvert.DeserializeXmlNode("{\"root\":" + s + "}", "root"); string xml = doc.InnerXml; XmlToCsvUsingDataSetFromString converter = new XmlToCsvUsingDataSetFromString(xml, (parameters.ContainsKey("qualifySep")) ? parameters["qualifySep"] : null); XmlToCsvContext context = new XmlToCsvContext(converter); foreach (DataTable dt in converter.XmlDataSet.Tables) { QvxLog.Log(QvxLogFacility.Application, QvxLogSeverity.Notice, "Found table " + dt.TableName + ": " + dt.Rows.Count.ToString()); List <QvxField> l = new List <QvxField>(); foreach (DataColumn dc in dt.Columns) { l.Add(new QvxField(dc.ColumnName, QvxFieldType.QVX_TEXT, QvxNullRepresentation.QVX_NULL_FLAG_SUPPRESS_DATA, FieldAttrType.ASCII)); } lt.Add(new QvxTable { TableName = dt.TableName, GetRows = delegate() { return(GetJSONRows(dt, MTables, FindTable)); }, Fields = l.ToArray() }); } } return(lt); }
public IEnumerable <DataTable> getRawTables(Database database, Dictionary <string, string> args) { QvxLog.Log(QvxLogFacility.Application, QvxLogSeverity.Debug, "+ getRawTables()"); QvxLog.Log(QvxLogFacility.Application, QvxLogSeverity.Debug, String.Format("getRawTables() : {0}", args.Count)); QvxLog.Log(QvxLogFacility.Application, QvxLogSeverity.Debug, String.Format("getRawTables() : {0}", String.Join(", ", args.Select(kv => String.Format("{0} : {1}", kv.Key, kv.Value))))); string response = null; if (args["Location"] == "File") { response = File.ReadAllText(args["File Name"], Encoding.UTF8); } else { MyWebRequest q; string param = args["Method"] == "POST" ? args["Params"] : null; if (args.ContainsKey("Http Method")) { q = new MyWebRequest( String.Format( "{0}/{1}", args["Host"], args["Method"] ) , args["Http Method"], param, "None", null, null); } else { throw new ArgumentOutOfRangeException(); } QvxLog.Log(QvxLogFacility.Application, QvxLogSeverity.Debug, "getRawTables() : request prepared"); //string s = q.GetResponse(); //QvxLog.Log(QvxLogFacility.Application, QvxLogSeverity.Debug, String.Format("getRawTables() : response : {0}", s.Substring(0, 50))); response = q.GetResponse(); if (q.StatusCode != HttpStatusCode.OK) { response = null; } QvxLog.Log(QvxLogFacility.Application, QvxLogSeverity.Debug, String.Format("getRawTables() : {0}", q.StatusCode)); } List <QvxTable> lt = new List <QvxTable>(); if (response != null) { XmlDocument doc; using (JsonTextReader jr = new JsonTextReader(new StringReader(response))) { QvxLog.Log(QvxLogFacility.Application, QvxLogSeverity.Debug, String.Format("getRawTables() : deserializing document")); XmlNodeConverter converter = new XmlNodeConverter(); converter.DeserializeRootElementName = "root"; converter.WriteArrayAttribute = false; JsonSerializerSettings settings = new JsonSerializerSettings { Converters = new JsonConverter[] { converter } }; JsonSerializer jsonSerializer = JsonSerializer.CreateDefault(settings); jsonSerializer.CheckAdditionalContent = true; doc = (XmlDocument)jsonSerializer.Deserialize(jr, typeof(XmlDocument)); QvxLog.Log(QvxLogFacility.Application, QvxLogSeverity.Debug, String.Format("getRawTables() : {0}", doc.InnerXml.Substring(0, Math.Min(5000, doc.InnerXml.Length)))); } QvxLog.Log(QvxLogFacility.Application, QvxLogSeverity.Debug, String.Format("getRawTables() : JSON deserialized")); string qualifySep = null; if (args.ContainsKey("Qualify") && Convert.ToBoolean(args["Qualify"])) { qualifySep = args["Qualify Sep"]; } XmlToCsvUsingDataSetFromString csvConverter = new XmlToCsvUsingDataSetFromString(doc, qualifySep, "_"); XmlToCsvContext context = new XmlToCsvContext(csvConverter); QvxLog.Log(QvxLogFacility.Application, QvxLogSeverity.Debug, "getRawTables() : converted to xml"); foreach (DataTable dt in csvConverter.XmlDataSet.Tables) { QvxLog.Log(QvxLogFacility.Application, QvxLogSeverity.Debug, String.Format("getRawTables() : table found {0}", dt.TableName)); yield return(dt); } } QvxLog.Log(QvxLogFacility.Application, QvxLogSeverity.Debug, "- getRawTables()"); }
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; } }