/// <summary> /// Converts a JsonObject to XML string format. /// </summary> /// <param name="jsonObject">JsonObject</param> /// <param name="root">Value to wrap XML in case is necesary. /// More info https://www.newtonsoft.com/json/help/html/ConvertingJSONandXML.htm#! </param> /// <returns name="xmlString">XML formatted string converted from JsonObject</returns> /// <search> /// json, convert, xml, parse /// </search> public static string JsonToXML(Elements.JsonObject jsonObject, string root = "") { try { XmlDocument doc = (XmlDocument)JsonConvert.DeserializeXmlNode(jsonObject.ToString(), root); StringWriter sw = new StringWriter(); XmlTextWriter xw = new XmlTextWriter(sw); xw.Formatting = System.Xml.Formatting.Indented; doc.WriteTo(xw); return(sw.ToString()); } catch (Exception e) { string exceptionMsg = e.Message; string msg1 = "JSON root object has multiple properties"; string msg2 = "Root containig more than one property"; string msg3 = "DocumentElement"; if (exceptionMsg.Contains(msg1) || exceptionMsg.Contains(msg2) || exceptionMsg.Contains(msg3)) { throw new Exception("Root containig more than one property. Try wrapping the input on a JsonObject element or adding a value on the root input parameter"); } else { throw new Exception(e.Message); } } }
/// <summary> /// Parses a CSV formated string. It will return a list of JsonObjects /// Error will be thrown if parser fails. /// </summary> /// <param name="csvString">CSV formatted string</param> /// <returns name="jsonObjects">List of JsonObjets returned by the parser</returns> /// <search> /// json, parse, csv /// </search> public static List <Elements.JsonObject> CSVString(string csvString) { try { using (StringReader sr = new StringReader(csvString)) using (TextFieldParser parser = new TextFieldParser(sr)) { parser.TextFieldType = FieldType.Delimited; parser.SetDelimiters(","); List <Elements.JsonObject> jsonObjects = new List <Elements.JsonObject>(); string[] headers = null; while (!parser.EndOfData) { dynamic[] fields = parser.ReadFields(); if (headers == null) { headers = fields as string[]; } else { Elements.JsonObject jObject = Elements.JsonObject.ByKeysAndValues(headers.ToList(), fields.ToList(), false, JsonOption.None); jsonObjects.Add(jObject); } } return(jsonObjects); }} catch (Exception ex) { throw new Exception(ex.Message); } }
/// <summary> /// Writes the JsonObject or JsonArray to a XML file. /// </summary> /// <param name="jsonObject">JsonObject or JsonArray element</param> /// <param name="filepath">File path for XML file</param> /// <param name="root">Value to wrap XML in case is necesary. /// More info https://www.newtonsoft.com/json/help/html/ConvertingJSONandXML.htm#! </param> /// <returns name="filepath">Returns the filepath if write operation is succesful</returns> /// <search> /// json, parser, to file, xmlfile, xml /// </search> public static string ToXMLFile(Elements.JsonObject jsonObject, string filepath, string root = "") { try { string xml = Parse.JsonToXML(jsonObject, root); File.WriteAllText(filepath, xml.ToString()); return(filepath); } catch (Exception e) { throw new Exception(e.Message); } }