/// <summary> /// Export data to a range of different file formats; /// ask the user for filename and file format /// /// NOTE this has been replaced by the two-part scheme above: /// first get the filename, then the caller loads the data /// from the server, then it calls ExportToFile. /// </summary> /// <param name="doc"></param> /// <param name="ADialogTitle"></param> /// <param name="ADefaultExtension"></param> /// <returns></returns> public static bool ExportWithDialog(XmlDocument doc, string ADialogTitle, string ADefaultExtension = "yml") { // TODO: openoffice // See also http://sourceforge.net/apps/mediawiki/openpetraorg/index.php?title=Data_liberation SaveFileDialog DialogSave = new SaveFileDialog(); DialogSave.Filter = Catalog.GetString( "Text file (*.yml)|*.yml|XML file (*.xml)|*.xml|Excel file (*.xlsx)|*.xlsx|Spreadsheet file (*.csv)|*.csv"); DialogSave.DefaultExt = ADefaultExtension; string[] filterLines = DialogSave.Filter.Split(new char[] { '|' }); for (int count = 0; count < filterLines.Length; count++) { if (count % 2 == 1 && filterLines[count] == "*." + ADefaultExtension) { DialogSave.FilterIndex = count / 2 + 1; } } DialogSave.AddExtension = true; DialogSave.RestoreDirectory = true; DialogSave.Title = ADialogTitle; if (DialogSave.ShowDialog() == DialogResult.OK) { if (DialogSave.FileName.ToLower().EndsWith("xml")) { doc.Save(DialogSave.FileName); return(true); } else if (DialogSave.FileName.ToLower().EndsWith("csv")) { return(TCsv2Xml.Xml2Csv(doc, DialogSave.FileName)); } else if (DialogSave.FileName.ToLower().EndsWith("yml")) { return(TYml2Xml.Xml2Yml(doc, DialogSave.FileName)); } else if (DialogSave.FileName.ToLower().EndsWith("xlsx")) { using (FileStream fs = new FileStream(DialogSave.FileName, FileMode.Create)) { if (TCsv2Xml.Xml2ExcelStream(doc, fs, false)) { fs.Close(); return(true); } } return(false); } } return(false); }
/// <summary> /// this is necessary to be able to store XML files that can be parsed by DTD/Schema mechanisms; /// for YML, we have the name of an element not as an attribute, but the element name itself; /// this would never work for validating xml; therefore all Elements are called XmlElement, with attribute name /// </summary> /// <param name="ADoc"></param> /// <returns></returns> public static string XmlToString2(XmlDocument ADoc) { XmlDocument tempDoc = TYml2Xml.CreateXmlDocument(); MoveElementNameToAttribute(ADoc.DocumentElement, tempDoc.DocumentElement); StringWriter sw = new StringWriter(); XmlTextWriter xw = new XmlTextWriter(sw); tempDoc.WriteTo(xw); return(sw.ToString()); }
/// <summary> /// convert from all sorts of formats into xml document; /// shows a dialog to the user to select the file to import /// </summary> /// <returns></returns> public static XmlDocument ImportWithDialog(string ADialogTitle, out string AFilename) { // TODO support import from Excel and OpenOffice files // See also http://sourceforge.net/apps/mediawiki/openpetraorg/index.php?title=Data_liberation OpenFileDialog DialogOpen = new OpenFileDialog(); AFilename = ""; DialogOpen.Filter = Catalog.GetString( "Text file (*.yml)|*.yml|XML file (*.xml)|*.xml|Spreadsheet file (*.csv)|All supported file formats (*.yml, *.xml, *.csv)|*.csv;*.yml;*.xml|"); DialogOpen.FilterIndex = 4; DialogOpen.RestoreDirectory = true; DialogOpen.Title = ADialogTitle; if (DialogOpen.ShowDialog() == DialogResult.OK) { AFilename = DialogOpen.FileName; if (DialogOpen.FileName.ToLower().EndsWith("csv")) { // select separator, make sure there is a header line with the column captions/names TDlgSelectCSVSeparator dlgSeparator = new TDlgSelectCSVSeparator(true); Boolean fileCanOpen = dlgSeparator.OpenCsvFile(DialogOpen.FileName); if (!fileCanOpen) { throw new Exception(String.Format(Catalog.GetString("File {0} Cannot be opened."), DialogOpen.FileName)); } dlgSeparator.SelectedSeparator = StringHelper.GetCSVSeparator(dlgSeparator.FileContent); if (dlgSeparator.ShowDialog() == DialogResult.OK) { XmlDocument doc = TCsv2Xml.ParseCSVContent2Xml(dlgSeparator.FileContent, dlgSeparator.SelectedSeparator); return(doc); } } else if (DialogOpen.FileName.ToLower().EndsWith("xml")) { XmlDocument doc = new XmlDocument(); doc.Load(DialogOpen.FileName); return(doc); } else if (DialogOpen.FileName.ToLower().EndsWith("yml")) { TYml2Xml yml = new TYml2Xml(DialogOpen.FileName); return(yml.ParseYML2XML()); } } return(null); }
/// <summary> /// Put this (XML formatted) data on a local file /// </summary> /// <param name="doc">XML data to be exported</param> /// <param name="FileName">Filename from GetExportFilename, above</param> /// <returns>true if successful</returns> public static bool ExportTofile(XmlDocument doc, string FileName) { if (FileName.EndsWith("xml")) { doc.Save(FileName); return(true); } else if (FileName.EndsWith("csv")) { return(TCsv2Xml.Xml2Csv(doc, FileName)); } else if (FileName.EndsWith("yml")) { return(TYml2Xml.Xml2Yml(doc, FileName)); } return(false); }
/// <summary> /// Export data to a range of different file formats; /// ask the user for filename and file format /// /// NOTE this has been replaced by the two-part scheme above: /// first get the filename, then the caller loads the data /// from the server, then it calls ExportToFile. /// </summary> /// <param name="doc"></param> /// <param name="ADialogTitle"></param> /// <returns></returns> public static bool ExportWithDialog(XmlDocument doc, string ADialogTitle) { // TODO: TExcel excel = new TExcel(); // TODO: openoffice // See also http://sourceforge.net/apps/mediawiki/openpetraorg/index.php?title=Data_liberation SaveFileDialog DialogSave = new SaveFileDialog(); DialogSave.DefaultExt = "yml"; DialogSave.Filter = Catalog.GetString( "Text file (*.yml)|*.yml|XML file (*.xml)|*.xml|Petra export (*.ext)|*.ext|Spreadsheet file (*.csv)|*.csv"); DialogSave.AddExtension = true; DialogSave.RestoreDirectory = true; DialogSave.Title = ADialogTitle; if (DialogSave.ShowDialog() == DialogResult.OK) { if (DialogSave.FileName.ToLower().EndsWith("xml")) { doc.Save(DialogSave.FileName); return(true); } else if (DialogSave.FileName.ToLower().EndsWith("ext")) { return(false); } else if (DialogSave.FileName.ToLower().EndsWith("csv")) { return(TCsv2Xml.Xml2Csv(doc, DialogSave.FileName)); } else if (DialogSave.FileName.ToLower().EndsWith("yml")) { return(TYml2Xml.Xml2Yml(doc, DialogSave.FileName)); } } return(false); }
/// <summary> /// insert parameters into placeholders /// </summary> /// <param name="curNode"></param> /// <returns></returns> public Boolean processTemplateParameters(XmlNode curNode) { // add all attributes as template parameters SortedList <string, string> attrList = TYml2Xml.GetAttributes(curNode); foreach (string key in attrList.Keys) { // some placeholders should be replaced after all other processing, e.g. Initialise_Ledger AddToCodelet(key, attrList[key]); } // there might be some sequences (e.g. XMLFILES) List <XmlNode> children = TYml2Xml.GetChildren(curNode, true); foreach (XmlNode child in children) { string nodeValue = StringHelper.StrMerge(TYml2Xml.GetElements(child), ','); // some placeholders should be replaced after all other processing, e.g. Initialise_Ledger AddToCodelet(child.Name, nodeValue); } return(true); }
/// <summary> /// convert a CSV file to an XmlDocument. /// the first line is expected to contain the column names/captions, in quotes. /// from the header line, the separator can be determined, if the parameter ASeparator is empty /// </summary> public static XmlDocument ParseCSV2Xml(List <string> ALines, string ASeparator = null) { XmlDocument myDoc = TYml2Xml.CreateXmlDocument(); int LineCounter = 1; string headerLine = ALines[0]; string separator = ASeparator; if (string.IsNullOrEmpty(ASeparator)) { if (!headerLine.StartsWith("\"")) { throw new Exception(Catalog.GetString("Cannot open CSV file, because it is missing the header line.") + Environment.NewLine + Catalog.GetString("There must be a row with the column captions, at least the first caption must be in quotes.")); } else { // read separator from header line. at least the first column needs to be quoted separator = headerLine[StringHelper.FindMatchingQuote(headerLine, 0) + 2].ToString(); } } List <string> AllAttributes = new List <string>(); while (headerLine.Length > 0) { string attrName = StringHelper.GetNextCSV(ref headerLine, separator); if (attrName.Length == 0) { TLogging.Log("Csv2Xml: found empty column header, will not consider any following columns"); break; } if (attrName.Length > 1) { attrName = attrName[0] + StringHelper.UpperCamelCase(attrName, ' ', false, false).Substring(1); } // some characters are not allowed in the name of an XmlAttribute attrName = attrName.Replace("%", "percent"); attrName = attrName.Replace("-", "hyphen"); attrName = attrName.Replace("/", "slash"); attrName = attrName.Replace(" ", "space"); try { myDoc.CreateAttribute(attrName); } catch (Exception) { char[] arr = attrName.ToCharArray(); // filter only letters and digits arr = Array.FindAll <char>(arr, (c => (char.IsLetterOrDigit(c)))); attrName = new string(arr); } AllAttributes.Add(attrName); } LineCounter = 1; while (LineCounter < ALines.Count) { string line = ALines[LineCounter]; if (line.Trim().Length > 0) { SortedList <string, string> AttributePairs = new SortedList <string, string>(); foreach (string attrName in AllAttributes) { // support csv values that contain line breaks AttributePairs.Add(attrName, StringHelper.GetNextCSV(ref line, ALines, ref LineCounter, separator)); } string rowName = "Element"; if (AttributePairs.ContainsKey("name")) { rowName = AttributePairs["name"]; } XmlNode newNode = myDoc.CreateElement("", rowName, ""); if (AttributePairs.ContainsKey("childOf")) { XmlNode parentNode = TXMLParser.FindNodeRecursive(myDoc.DocumentElement, AttributePairs["childOf"]); if (parentNode == null) { parentNode = myDoc.DocumentElement; } parentNode.AppendChild(newNode); } else { myDoc.DocumentElement.AppendChild(newNode); } foreach (string attrName in AllAttributes) { if ((attrName != "name") && (attrName != "childOf")) { XmlAttribute attr = myDoc.CreateAttribute(attrName); attr.Value = AttributePairs[attrName]; newNode.Attributes.Append(attr); } } } LineCounter++; } return(myDoc); }