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