public void Execute() { CCPDataSet set = new CCPDataSet(); switch (ImportType) { case ImportType.Csv: set = CsvUtils.Read(ImportFilePath); break; case ImportType.Xml: set = XmlUtils.Read(ImportFilePath); break; case ImportType.Json: set = JsonUtils.Read(ImportFilePath); break; } switch (ExportType) { case ImportType.Csv: CsvUtils.Convert(set, ExportFilePath); break; case ImportType.Xml: XmlUtils.Convert(set, ExportFilePath); break; case ImportType.Json: JsonUtils.Convert(set, ExportFilePath); break; } }
public static void Convert(CCPDataSet set, string exportFilePath) { List <IDictionary <string, object> > outputSet = new List <IDictionary <string, object> >(); //Each row foreach (var row in set.Rows) { IDictionary <string, object> parent = new ExpandoObject(); //Each property in the row foreach (var subItem in row.Items) { if (subItem.Name.Contains('_')) { var splitted = subItem.Name.Split('_'); var group = splitted[0]; var groupedItem = splitted[1]; //We're using ExpandoObjects since we can dynamically add properties as indexed entries, wrapping them as IDictionary types IDictionary <string, object> child = new ExpandoObject(); try { child = ((IDictionary <string, object>)parent[group]); child.Add(groupedItem, subItem.Value); } catch (Exception) { child[groupedItem] = subItem.Value; } parent[group] = child; } else { parent[subItem.Name] = subItem.Value; } } outputSet.Add(parent); } string jsonString = JsonConvert.SerializeObject(outputSet); using FileStream fs = File.OpenWrite(exportFilePath); byte[] bytes = Encoding.UTF8.GetBytes(jsonString); fs.Write(bytes, 0, bytes.Length); }
public static CCPDataSet Read(string importFilePath) { CCPDataSet set = new CCPDataSet(); if (File.Exists(importFilePath)) { //Read out our json into a new set for interrogation var entries = JsonConvert.DeserializeObject <List <Dictionary <string, object> > >(File.ReadAllText(importFilePath)); foreach (var entry in entries) { CCPDataRow row = new CCPDataRow(); foreach (var field in entry) { if (field.Value.ToString().StartsWith("{") && field.Value.ToString().EndsWith("}")) { //We're looking at a collection here var nest = JsonConvert.DeserializeObject <Dictionary <string, string> >(field.Value.ToString()); foreach (var n in nest) { CCPDataItem item = new CCPDataItem(); item.Name = $"{field.Key}_{n.Key}"; item.Value = n.Value.ToString(); row.Items.Add(item); } } else { //Basic item, nothing fancy CCPDataItem item = new CCPDataItem(); item.Name = field.Key; item.Value = field.Value.ToString(); row.Items.Add(item); } } set.Rows.Add(row); } } return(set); }
public static CCPDataSet Read(string importFilePath) { CCPDataSet set = new CCPDataSet(); if (File.Exists(importFilePath)) { //Load xml into memory XmlDocument doc = new XmlDocument(); doc.Load(importFilePath); foreach (XmlNode n in doc.DocumentElement) { CCPDataRow row = new CCPDataRow(); foreach (XmlNode r in n.ChildNodes) { //If we have child nodes, and those child nodes are actually xml elements, that means we want to dig deeper if (r.HasChildNodes && r.ChildNodes.OfType <XmlElement>().Any()) { foreach (XmlNode cc in r.ChildNodes) { CCPDataItem item = new CCPDataItem(); item.Name = $"{r.Name}_{cc.Name}"; item.Value = cc.InnerText; row.Items.Add(item); } } else { //Otherwise, we're as deep as we want to be CCPDataItem item = new CCPDataItem(); item.Name = r.Name; item.Value = r.InnerText; row.Items.Add(item); } } set.Rows.Add(row); } } return(set); }
public static void Convert(CCPDataSet set, string exportFilePath) { var headers = set.Rows.First().Items.Select(x => x.Name).ToList(); StringBuilder sb = new StringBuilder(); sb.AppendLine(string.Join(',', headers)); foreach (var row in set.Rows) { sb.AppendLine(string.Join(',', row.Items.Select(x => x.Value))); } var csvString = sb.ToString(); using FileStream fs = File.OpenWrite(exportFilePath); byte[] bytes = Encoding.UTF8.GetBytes(csvString); fs.Write(bytes, 0, bytes.Length); }
public static CCPDataSet Read(string importFilePath) { CCPDataSet set = new CCPDataSet(); if (File.Exists(importFilePath)) { using (TextFieldParser parser = new TextFieldParser(importFilePath)) { parser.SetDelimiters(new string[] { "," }); string[] headers = parser.ReadFields(); while (!parser.EndOfData) { var row = new CCPDataRow(); string[] properties = parser.ReadFields(); int index = 0; foreach (var p in properties) { var headerName = headers[index]; var header = new CCPDataItem(); header.Name = headers[index]; header.Value = p; header.OrderIndex = index; row.Items.Add(header); index++; } set.Rows.Add(row); } } } return(set); }
public static void Convert(CCPDataSet set, string exportFilePath) { //Set up some settings to make our XML nicer XmlWriterSettings settings = new XmlWriterSettings(); settings.OmitXmlDeclaration = true; settings.IndentChars = "\t"; settings.Indent = true; settings.CloseOutput = true; string xmlString; using (StringWriter sw = new StringWriter()) { using (XmlWriter writer = XmlWriter.Create(sw)) { //Container element writer.WriteStartElement("row"); foreach (var row in set.Rows) { //Another container element writer.WriteStartElement("item"); bool openElement = false; for (int i = 0; i < row.Items.Count; i++) { var subItem = row.Items[i]; //If the name has a hyphen, we want to have a nested item if (subItem.Name.Contains('_')) { //Split out the name into the parent/child relationship var split = subItem.Name.Split('_'); var group = split[0]; var groupedItem = split[1]; //If we're just starting this nest, open up an appropriate element if (!openElement) { writer.WriteStartElement(group); openElement = true; } //Slam the info in writer.WriteElementString(groupedItem, subItem.Value); //If we have more items to go, check the next one if (i < row.Items.Count - 1) { var nextItem = row.Items[i + 1]; if (nextItem.Name.Contains('_') && nextItem.Name.Split('_')[0] == group) { //The next item is also nested and belongs to this } else if (openElement) { //We're dealing with a different element or nest, so we can close this element, if its open writer.WriteEndElement(); openElement = false; } } else if (openElement) { //We're at the end of a row and need to end the element if it's open writer.WriteEndElement(); openElement = false; } } else { //In this case, just a plain element writer.WriteElementString(subItem.Name, subItem.Value); } } writer.WriteEndElement(); } writer.WriteEndElement(); } xmlString = sw.ToString(); } using FileStream fs = File.OpenWrite(exportFilePath); byte[] bytes = Encoding.UTF8.GetBytes(xmlString); fs.Write(bytes, 0, bytes.Length); }