public static string ToText <TRec>(TRec record, ChoYamlRecordConfiguration configuration = null, TraceSwitch traceSwitch = null, string yamlPath = null) { if (record is DataTable) { StringBuilder xml = new StringBuilder(); using (var w = new ChoYamlWriter(xml, configuration)) w.Write(record as DataTable); return(xml.ToString()); } else if (record is IDataReader) { StringBuilder xml = new StringBuilder(); using (var w = new ChoYamlWriter(xml, configuration)) w.Write(record as IDataReader); return(xml.ToString()); } if (configuration == null) { configuration = new ChoYamlRecordConfiguration(); } if (configuration.SingleDocument == null) { configuration.SingleDocument = true; } return(ToTextAll(ChoEnumerable.AsEnumerable <TRec>(record), configuration, traceSwitch, yamlPath)); }
public static string ToTextAll <TRec>(IEnumerable <TRec> records, ChoYamlRecordConfiguration configuration = null, TraceSwitch traceSwitch = null, string yamlPath = null) { if (records == null) { return(null); } if (typeof(DataTable).IsAssignableFrom(typeof(TRec))) { StringBuilder json = new StringBuilder(); foreach (var dt in records.Take(1)) { using (var w = new ChoYamlWriter(json, configuration)) { w.Write(dt); } } return(json.ToString()); } else if (typeof(IDataReader).IsAssignableFrom(typeof(TRec))) { StringBuilder json = new StringBuilder(); foreach (var dt in records.Take(1)) { using (var w = new ChoYamlWriter(json, configuration)) { w.Write(dt); } } return(json.ToString()); } using (var stream = new MemoryStream()) using (var reader = new StreamReader(stream)) using (var writer = new StreamWriter(stream)) using (var parser = new ChoYamlWriter <TRec>(writer, configuration) { TraceSwitch = traceSwitch == null ? ChoETLFramework.TraceSwitch : traceSwitch }) { parser.Configuration.YamlPath = yamlPath; parser.Write(records); parser.Close(); writer.Flush(); stream.Position = 0; return(reader.ReadToEnd()); } }
internal static string ToText(object rec, ChoYamlRecordConfiguration configuration, Encoding encoding, int bufferSize, TraceSwitch traceSwitch = null) { if (rec is DataTable) { StringBuilder json = new StringBuilder(); using (var w = new ChoYamlWriter(json, configuration)) { w.Write(rec as DataTable); } return(json.ToString()); } else if (rec is IDataReader) { StringBuilder json = new StringBuilder(); using (var w = new ChoYamlWriter(json, configuration)) { w.Write(rec as IDataReader); } return(json.ToString()); } ChoYamlRecordWriter writer = new ChoYamlRecordWriter(rec.GetType(), configuration); writer.TraceSwitch = traceSwitch == null ? ChoETLFramework.TraceSwitchOff : traceSwitch; using (var stream = new MemoryStream()) using (var reader = new StreamReader(stream)) using (var sw = new StreamWriter(stream, configuration.Encoding, configuration.BufferSize)) { writer.WriteTo(sw, new object[] { rec }).Loop(); sw.Flush(); stream.Position = 0; return(reader.ReadToEnd()); } }