static void QuickDynamicTest() { ArrayList al = new ArrayList(); al.Add(1); al.Add("abc"); List <int> lint = new List <int>() { 1, 2 }; Hashtable ht = new Hashtable(); ht.Add(1, "abc"); ChoSerializableDictionary <int, string> dict = new ChoSerializableDictionary <int, string>(); dict.Add(1, "abc"); List <ExpandoObject> objs = new List <ExpandoObject>(); dynamic rec1 = new ExpandoObject(); rec1.Id = 1; rec1.Name = "Mark"; rec1.IsActive = true; rec1.Message = new ChoCDATA("Test"); rec1.Array = al; rec1.Lint = lint; //rec1.HT = ht; rec1.Dict = dict; objs.Add(rec1); dynamic rec2 = new ExpandoObject(); rec2.Id = 2; rec2.Name = "Jason"; rec2.IsActive = true; rec2.Message = new ChoCDATA("Test"); objs.Add(rec2); StringBuilder sb = new StringBuilder(); using (var parser = new ChoXmlWriter(sb).WithXPath("Employees/Employee")) { parser.Write(objs); } Console.WriteLine(sb.ToString()); var a = ChoXmlReader.LoadText(sb.ToString()).ToArray(); var config = new ChoXmlRecordConfiguration(); //config.Configure(c => c.RootName = "Root"); Console.WriteLine(ChoXmlWriter.ToText(a.First(), config)); }
static void Test2() { StringBuilder xml = new StringBuilder(); using (var parser = new ChoXmlWriter(xml).WithXPath("//ticket//Employees/Employee") ) { parser.Write(new object[] { new { Id = 1, Name = "Mark" }, new { Id = 1, Name = "Mark" } }); } Console.WriteLine(xml.ToString()); }
static void Sample3() { using (var jr = new ChoJSONReader <MyObjectType>("sample3.json").WithJSONPath("$.menu") ) { jr.AfterRecordFieldLoad += (o, e) => { }; using (var xw = new ChoXmlWriter <MyObjectType>("sample3.xml").Configure(c => c.UseXmlSerialization = true)) xw.Write(jr); } }
static void DataReaderTest() { string connectionstring = @"Data Source=(localdb)\v11.0;Initial Catalog=TestDb;Integrated Security=True"; using (var conn = new SqlConnection(connectionstring)) { conn.Open(); var comm = new SqlCommand("SELECT * FROM Customers", conn); using (var parser = new ChoXmlWriter("customers.xml").WithXPath("Customers/Customer")) parser.Write(comm.ExecuteReader()); } }
static void DictTest() { StringBuilder xml = new StringBuilder(); using (var w = new ChoXmlWriter(xml).UseXmlSerialization().WithXPath("Employees/Employee")) { w.Write(new Dictionary <int, string> { { 1, "Tom" }, { 2, "Mark" } }); } Console.WriteLine(xml.ToString()); }
static void ListTest() { StringBuilder xml = new StringBuilder(); using (var w = new ChoXmlWriter <string>(xml).UseXmlSerialization().WithXPath("t1/t2/r4/t5/Employees/Employee")) { w.Write(new List <string> { "Tom", "Mark" }); } Console.WriteLine(xml.ToString()); }
public static void ConfigFirstTest() { string expected = @"<Employees> <Employee> <Id>1</Id> <Name>Mark</Name> </Employee> <Employee> <Id>2</Id> <Name /> </Employee> </Employees>"; string actual = null; List <ExpandoObject> objs = new List <ExpandoObject>(); dynamic rec1 = new ExpandoObject(); rec1.Id = 1; rec1.Name = "Mark"; rec1.IsActive = true; rec1.Message = new ChoCDATA("Test"); objs.Add(rec1); dynamic rec2 = new ExpandoObject(); rec2.Id = 2; rec2.Name = null; rec2.IsActive = true; rec2.Message = new ChoCDATA("Test"); objs.Add(rec2); ChoXmlRecordConfiguration config = new ChoXmlRecordConfiguration(); config.XmlRecordFieldConfigurations.Add(new ChoXmlRecordFieldConfiguration("Id")); config.XmlRecordFieldConfigurations.Add(new ChoXmlRecordFieldConfiguration("Name")); using (var stream = new MemoryStream()) using (var reader = new StreamReader(stream)) using (var writer = new StreamWriter(stream)) using (var parser = new ChoXmlWriter(writer, config).WithXPath("Employees/Employee")) { parser.Write(objs); writer.Flush(); stream.Position = 0; actual = reader.ReadToEnd(); } Assert.AreEqual(expected, actual); }
public static void QuickPOCOTest() { List <EmployeeRecSimple> objs = new List <EmployeeRecSimple>(); EmployeeRecSimple rec1 = new EmployeeRecSimple(); rec1.Id = null; rec1.Name = "Mark"; rec1.Depends = new List <string>() { "AA", "BB" }; rec1.Courses = new Dictionary <int, string>() { { 1, "AA" }, { 2, "BB" } }; objs.Add(rec1); EmployeeRecSimple rec2 = new EmployeeRecSimple(); rec2.Id = "2"; rec2.Name = null; objs.Add(rec2); objs.Add(null); using (var parser = new ChoXmlWriter <EmployeeRecSimple>(FileNameQuickPOCOTestActualXML).WithXPath("Employees/Employee") .Configure(e => e.NullValueHandling = ChoNullValueHandling.Default) ) { parser.Write(objs); } FileAssert.AreEqual(FileNameQuickPOCOTestExpectedXML, FileNameQuickPOCOTestActualXML); // using (var reader = new ChoXmlReader("emp.xml").WithXPath("Employees/Employee") //.WithField("Id") //.WithField("Name") //.WithField("Depends", isArray: false, fieldType: typeof(List<string>)) //.WithField("KVP", isArray: false, fieldType: typeof(List<ChoKeyValuePair<int, string>>)) //) // { // foreach (var i in reader) // Console.WriteLine(ChoUtility.ToStringEx(i)); // } //using (var reader = new ChoXmlReader<EmployeeRecSimple>("emp.xml").WithXPath("Employees/Employee")) //{ // foreach (var i in reader) // Console.WriteLine(ChoUtility.ToStringEx(i)); //} }
public static void SaveDict() { //Dictionary<int, string> list = new Dictionary<int, string>(); //list.Add(1, "1/1/2012"); //list.Add(2, null); Hashtable list = new Hashtable(); list.Add(1, "33"); list.Add(2, null); using (var w = new ChoXmlWriter("emp.xml") ) w.Write(list); }
static void JsonToXml() { using (var csv = new ChoXmlWriter("companies.xml") { TraceSwitch = ChoETLFramework.TraceSwitchOff }.WithXPath("companies/company")) { csv.Write(new ChoJSONReader <Company>("companies.json") { TraceSwitch = ChoETLFramework.TraceSwitchOff }.NotifyAfter(10000).Take(10). SelectMany(c => c.Products.Touch(). Select(p => new { c.name, c.Permalink, prod_name = p.name, prod_permalink = p.Permalink }))); } }
static void CustomMemberSerialization() { var sb = new StringBuilder(); using (var p = new ChoXmlWriter <Choice>(sb) .WithField("Options", valueConverter: o => String.Join(",", o as string[])) ) { List <Choice> l = new List <Choice> { new Choice { Options = new[] { "op 1", "op 2" }, EmpArr = new Emp[] { new Emp { Id = 1, Name = "Tom" }, new Emp { Id = 2, Name = "Mark" }, null }, Emp = new Emp { Id = 0, Name = "Raj" }, //EmpDict = new Dictionary<int, Emp> { { 1, new Emp { Id = 11, Name = "Tom1" } } }, Ids = new List <int> { 1, 2, 3 } }, new Choice { Options = new[] { "op 1", "op 2" }, EmpArr = new Emp[] { new Emp { Id = 1, Name = "Tom" }, new Emp { Id = 2, Name = "Mark" }, null }, Emp = new Emp { Id = 0, Name = "Raj" }, //EmpDict = new Dictionary<int, Emp> { { 1, new Emp { Id = 11, Name = "Tom1" } } }, Ids = new List <int> { 1, 2, 3 } } }; p.Write(l); } Console.WriteLine(sb.ToString()); //Console.WriteLine(ChoXmlWriter.ToText<Choice>(new Choice { Options = new[] { "op 1", "op 2" } })); }
public static void SaveStringList() { //List<string> list = new List<string>(); //list.Add("1/1/2012"); //list.Add(null); ArrayList list = new ArrayList(); list.Add(1); list.Add("asas"); list.Add(null); using (var w = new ChoXmlWriter("emp.xml") .WithField("Value") ) w.Write(list); }
public static void CustomSerialization() { string expected = @"<Root> <XElement id=""dd""> <address>dd</address> </XElement> <XElement id=""dd""> <address>dd</address> </XElement> </Root>"; string actual = null; dynamic address = new ChoDynamicObject(); address.Street = "10 River Rd"; address.City = "Princeton"; dynamic state = new ChoDynamicObject(); state.State = "NJ"; state.Zip = "09930"; address.State = state; StringBuilder sb = new StringBuilder(); using (var w = new ChoXmlWriter(sb) .WithXmlAttributeField("id") .WithXmlElementField("address") .Setup(s => s.RecordFieldWriteError += (o, e) => Console.WriteLine(e.Exception.ToString())) .Setup(s => s.RecordFieldSerialize += (o, e) => { e.Source = "dd"; //e.Source = "<{0}>DD</{0}>".FormatString(e.PropertyName); //e.Handled = true; }) ) { //w.Write(new KeyValuePair<int, string>(1, "MM")); //w.Write(new KeyValuePair<int, string>(1, "MM")); w.Write(new { id = "2s->", address = address }); w.Write(new { id = "1s->", address = address }); } actual = sb.ToString(); Assert.AreEqual(expected, actual); }
public static void Sample7Test() { using (var jr = new ChoJSONReader(FileNameSample7JSON).WithJSONPath("$.fathers") .WithField("id") .WithField("married", fieldType: typeof(bool)) .WithField("name") .WithField("sons") ) { using (var w = new ChoXmlWriter(FileNameSample7ActualXML)) { w.Write(jr); } } FileAssert.AreEqual(FileNameSample7ExpectedXML, FileNameSample7ActualXML); }
static void RootAttributeWithNSTest() { StringBuilder xml = new StringBuilder(); using (var w = new ChoXmlWriter <FooBar>(xml) //.WithXmlNamespace("", "urn:foobar1") .Configure(c => c.DoNotEmitXmlNamespace = true) ) { w.Write(new FooBar { Foo = "Tom" }); } Console.WriteLine(xml.ToString()); }
public static void SaveDict() { //Dictionary<int, string> list = new Dictionary<int, string>(); //list.Add(1, "1/1/2012"); //list.Add(2, null); Hashtable list = new Hashtable(); list.Add(1, "33"); list.Add(2, null); StringBuilder msg = new StringBuilder(); using (var w = new ChoXmlWriter(msg) ) w.Write(list); Console.WriteLine(msg.ToString()); }
static void Main(string[] args) { XmlToCSVSample(); return; //dynamic p = new ChoPropertyBag(); //p.Name = "Raj"; //p.Zip = "10020"; //foreach (var kvp in ChoExpandoObjectEx.ToExpandoObject(p)) // Console.WriteLine(kvp); //return; //XmlToJSONSample4(); // JSONToXmlSample4(); string json = @" { ""Row1"":{ ""x"":123, ""y"":21, ""z"":22 }, }"; using (var r = ChoJSONReader.LoadText(json) .WithField("x", jsonPath: "$.Row1", fieldType: typeof(object)) .WithField("y", jsonPath: "$.Row1.y", fieldType: typeof(object)) .WithField("z", jsonPath: "$.Row1", fieldType: typeof(object)) ) { r.BeforeRecordFieldLoad += (o, e) => { if (e.PropertyName == "x") { //dynamic x = e.PropertyName; //e.Record.x = x.x; e.Skip = true; } }; using (var writer = new ChoXmlWriter("sample.xml")) writer.Write(r); return; } }
static void MergeXml() { IDictionary <string, dynamic[]> lookup = null; using (var r2 = new ChoXmlReader("XMLFile2.xml") .WithXPath("//Transaction") ) { lookup = r2.GroupBy(rec1 => (string)rec1.JID).ToDictionary(lu => lu.Key, lu => lu.ToArray()); } dynamic[] nodes = null; using (var r1 = new ChoXmlReader("XMLFile1.xml") .WithXPath("//Journals") ) { nodes = r1.ToArray(); } foreach (var rec in nodes) { if (lookup.ContainsKey((string)rec.JournalID)) { rec.Add("Transactions", lookup[(string)rec.JournalID]); } } foreach (var rec in nodes) { Console.WriteLine(rec.Dump()); } StringBuilder xml = new StringBuilder(); using (var w = new ChoXmlWriter(xml) .Configure(c => c.UseXmlArray = false) ) { w.Write(nodes); } Console.WriteLine(xml.ToString()); }
public static void SaveStringList() { //List<string> list = new List<string>(); //list.Add("1/1/2012"); //list.Add(null); ArrayList list = new ArrayList(); list.Add(1); list.Add("asas"); list.Add(null); StringBuilder msg = new StringBuilder(); using (var w = new ChoXmlWriter(msg) ) w.Write(list); Console.WriteLine(msg.ToString()); }
static void KVPTest() { StringBuilder msg = new StringBuilder(); using (var xr = new ChoXmlWriter(msg) .Configure(c => c.NamespaceManager = null) .Configure(c => c.NullValueHandling = ChoNullValueHandling.Empty) //.Configure(c => c.RootName = "KVP") //.Configure(c => c.NodeName = "KeyValue") ) { xr.Write((KeyValuePair <string, int>?)null); //xr.Write(new KeyValuePair<string, int>("X1", 1)); xr.Write(1); xr.Write(2); //xr.Write(new KeyValuePair<string, int>("X2", 2)); } Console.WriteLine(msg.ToString()); }
public static void CSVWithSpaceHeader2Xml() { string csv = @"Id, First Name 1, Tom 2, Mark"; StringBuilder xml = new StringBuilder(); using (var r = ChoCSVReader.LoadText(csv) .WithFirstLineHeader()) { using (var w = new ChoXmlWriter(xml) //.TurnOffAutoCorrectXNames() .ErrorMode(ChoErrorMode.ThrowAndStop) ) { w.Write(r); } } Console.WriteLine(xml.ToString()); //using (var reader = new ChoCSVReader("C:\\Server Media\\test3.csv") // .WithFirstLineHeader() // .Configure(c => c.FileHeaderConfiguration.IgnoreColumnsWithEmptyHeader = true) // ) //{ // using (var writer = new ChoXmlWriter(sb) // .Configure(c => c.RootName = "Records") // .Configure(c => c.NodeName = "Record") // .Configure(c => c.EmptyXmlNodeValueHandling = ChoEmptyXmlNodeValueHandling.Empty) // .Configure(c => c.ErrorMode = ChoErrorMode.ThrowAndStop) // ) // { // writer.Write(reader.Select(r => // { // r.RenameKey("Company Name", "CompanyName"); // return r; // })); // } //} }
static void DataTableTest() { string connectionstring = @"Data Source=(localdb)\v11.0;Initial Catalog=TestDb;Integrated Security=True"; using (var conn = new SqlConnection(connectionstring)) { conn.Open(); var comm = new SqlCommand("SELECT * FROM Customers", conn); SqlDataAdapter adap = new SqlDataAdapter(comm); DataTable dt = new DataTable("Customer"); adap.Fill(dt); using (var parser = new ChoXmlWriter("customers.xml").WithXPath("Customers/Customer").Configure(c => c.XmlRecordFieldConfigurations.Add(new ChoXmlRecordFieldConfiguration("CustId") { IsXmlAttribute = true }))) parser.Write(dt); } }
public static void Sample14() { using (var w = new ChoXmlWriter("sample14out.xml")) { using (var parser = new ChoXmlReader("sample14.xml") ) { foreach (dynamic rec in parser) { //dynamic x = rec.description; //rec.description = new ChoDynamicObject(); //rec.description.val = "100"; //rec.description.Value = new FamilyMember(); w.Write(rec); Console.WriteLine(ChoUtility.Dump(rec)); } } } }
static void QuickPOCOTest() { List <EmployeeRecSimple> objs = new List <EmployeeRecSimple>(); EmployeeRecSimple rec1 = new EmployeeRecSimple(); rec1.Id = 1; rec1.Name = "Mark"; objs.Add(rec1); EmployeeRecSimple rec2 = new EmployeeRecSimple(); rec2.Id = 2; rec2.Name = "Jason"; objs.Add(rec2); using (var parser = new ChoXmlWriter <EmployeeRecSimple>("Emp.xml").WithXPath("Employees/Employee")) { parser.Write(objs); } }
public static void CustomStringArrayTest() { string expected = @"<Root> <Days> <Monday /> <Tuesday /> <Wed /> </Days> </Root>"; string actual = null; List <string> s = new List <string>(); s.Add("Monday"); s.Add("Tuesday"); s.Add("Wed"); StringBuilder sb = new StringBuilder(); using (var w = new ChoXmlWriter(sb) .Configure(c => c.IgnoreNodeName = true) .WithField("Days", customSerializer: (v) => { StringBuilder sb1 = new StringBuilder(); sb1.AppendLine("<Days>"); foreach (var r in (IList)v) { sb1.AppendLine("<{0} />".FormatString(r).Indent(2, " ")); } sb1.Append("</Days>"); return(sb1.ToString()); }) ) w.Write(new { Days = s }); actual = sb.ToString(); Assert.AreEqual(expected, actual); }
static void Yaml2XmlTest() { string yaml = @" emps: - id: 1 name: Tom - id: 2 name: Mark "; StringBuilder xml = new StringBuilder(); using (var r = ChoYamlReader.LoadText(yaml).WithYamlPath("$.emps[*]")) { using (var w = new ChoXmlWriter(xml) .WithRootName("Emps") .WithNodeName("Emp") ) w.Write(r); } Console.WriteLine(xml.ToString()); }
static void ConfigFirstTest() { List <ExpandoObject> objs = new List <ExpandoObject>(); dynamic rec1 = new ExpandoObject(); rec1.Id = 1; rec1.Name = "Mark"; rec1.IsActive = true; rec1.Message = new ChoCDATA("Test"); objs.Add(rec1); dynamic rec2 = new ExpandoObject(); rec2.Id = 2; rec2.Name = null; rec2.IsActive = true; rec2.Message = new ChoCDATA("Test"); objs.Add(rec2); ChoXmlRecordConfiguration config = new ChoXmlRecordConfiguration(); config.XmlRecordFieldConfigurations.Add(new ChoXmlRecordFieldConfiguration("Id")); config.XmlRecordFieldConfigurations.Add(new ChoXmlRecordFieldConfiguration("Name")); using (var stream = new MemoryStream()) using (var reader = new StreamReader(stream)) using (var writer = new StreamWriter(stream)) using (var parser = new ChoXmlWriter(writer, config).WithXPath("Employees/Employee")) { parser.Write(objs); writer.Flush(); stream.Position = 0; Console.WriteLine(reader.ReadToEnd()); } }
public static void POCOCSVW2XmlNoFormattingTest() { string csv = @"Id, Name 1, Tom 2, Mark"; StringBuilder xml = new StringBuilder(); using (var r = ChoCSVReader <Emp> .LoadText(csv) .WithFirstLineHeader()) { using (var w = new ChoXmlWriter <Emp>(xml) .ErrorMode(ChoErrorMode.ThrowAndStop) //.IgnoreRootName() //.IgnoreNodeName() //.Configure(c => c.Formatting = System.Xml.Formatting.None) ) { w.Write(r.First()); } } Console.WriteLine(xml.ToString()); }
public static void Json2Xml1() { string json = @" [ { ""firstName"": ""John"", ""lastName"": ""Smith"", ""age"": 25, ""address"": { ""streetAddress"": ""21 2nd Street"", ""city"": ""New York"", ""state"": ""NY"", ""postalCode"": ""10021"" }, ""phoneNumber"": [ { ""type"": ""home"", ""number"": ""212 555-1234"" }, { ""type"": ""fax"", ""number"": ""646 555-4567"" } ] }, { ""firstName"": ""Tom"", ""lastName"": ""Mark"", ""age"": 50, ""address"": { ""streetAddress"": ""10 Main Street"", ""city"": ""Edison"", ""state"": ""NJ"", ""postalCode"": ""08837"" }, ""phoneNumber"": [ { ""type"": ""home"", ""number"": ""732 555-1234"" }, { ""type"": ""fax"", ""number"": ""609 555-4567"" } ] } ] "; using (var r = ChoJSONReader.LoadText(json)) { using (var w = new ChoXmlWriter(Console.Out) .UseXmlSerialization() .Configure(c => c.OmitXsiNamespace = true) .ErrorMode(ChoErrorMode.IgnoreAndContinue) ) { w.Write(r); } } }
public static void QuickDynamicTest() { string expected = @"<Employees> <Employee> <Id>1</Id> <Name>Mark</Name> <IsActive>true</IsActive> <Message><![CDATA[Test]]></Message> <Array> <anyType xmlns:q1=""http://www.w3.org/2001/XMLSchema"" p3:type=""q1:int"" xmlns:p3=""http://www.w3.org/2001/XMLSchema-instance"">1</anyType> <anyType xmlns:q2=""http://www.w3.org/2001/XMLSchema"" p3:type=""q2:string"" xmlns:p3=""http://www.w3.org/2001/XMLSchema-instance"">abc</anyType> </Array> <Lint> <int>1</int> <int>2</int> </Lint> <Dict> <item> <key> <int>1</int> </key> <value> <string>abc</string> </value> </item> </Dict> </Employee> <Employee> <Id>2</Id> <Name>Jason</Name> <IsActive>true</IsActive> <Message><![CDATA[Test]]></Message> </Employee> </Employees>"; string actual = null; ArrayList al = new ArrayList(); al.Add(1); al.Add("abc"); List <int> lint = new List <int>() { 1, 2 }; Hashtable ht = new Hashtable(); ht.Add(1, "abc"); ChoSerializableDictionary <int, string> dict = new ChoSerializableDictionary <int, string>(); dict.Add(1, "abc"); List <ExpandoObject> objs = new List <ExpandoObject>(); dynamic rec1 = new ExpandoObject(); rec1.Id = 1; rec1.Name = "Mark"; rec1.IsActive = true; rec1.Message = new ChoCDATA("Test"); rec1.Array = al; rec1.Lint = lint; //rec1.HT = ht; rec1.Dict = dict; objs.Add(rec1); dynamic rec2 = new ExpandoObject(); rec2.Id = 2; rec2.Name = "Jason"; rec2.IsActive = true; rec2.Message = new ChoCDATA("Test"); objs.Add(rec2); StringBuilder sb = new StringBuilder(); using (var parser = new ChoXmlWriter(sb).WithXPath("Employees/Employee")) { parser.Write(objs); } actual = sb.ToString(); Assert.AreEqual(expected, actual); //var a = ChoXmlReader.LoadText(sb.ToString()).ToArray(); //var config = new ChoXmlRecordConfiguration(); ////config.Configure(c => c.RootName = "Root"); //Console.WriteLine(ChoXmlWriter.ToText(a.First(), config)); }