示例#1
0
 private static void Sample2()
 {
     using (var csvWriter = new ChoCSVWriter("sample2.csv").WithFirstLineHeader())
     {
         using (var xmlReader = new ChoXmlReader("sample2.xml", "http://tempuri.org").WithXPath("/impots/impot")
                                .WithField("impotno", xPath: "x:original-impot-no")
                                .WithField("productlineitem", xPath: "x:product-lineitems/x:product-lineitem")
                                //.Setup(s => s.BeforeRecordFieldLoad += (o, e) =>
                                //{
                                //    var x = e;
                                //})
                )
         {
             foreach (dynamic i in xmlReader)
             {
                 foreach (dynamic pl in i.productlineitem)
                 {
                     Console.WriteLine(i.impotno + " " + pl.price);
                 }
             }
             //csvWriter.Write(xmlReader.SelectMany(rec => ((IEnumerable<dynamic>)rec.Smallprice).Select(rec1 => new { rec.originalimpotno, rec1.Small_price })));
             //csvWriter.Write(xmlReader.SelectMany(rec => ((IEnumerable<dynamic>)rec.productlineitem).Select(rec1 => new
             //{
             //    ImportNo = rec.impotno,
             //    Price = rec1.price,
             //    SmallPrice = rec1.Small_price,
             //    BigPrice = rec1.Big_price
             //})));
         }
     }
 }
示例#2
0
        public void XML(Stream fileStream)
        {
            var stream = new StreamReader(fileStream);

            using (stream)
            {
                using (var parser = new ChoXmlReader(stream))
                {
                    dynamic row;
                    while ((row = parser.Read()) != null)
                    {
                        var paymentDto = new PaymentModel()
                        {
                            Id = row.Id,
                            TransactionDate = Convert.ToDateTime(row.TransactionDate),
                            Amount          = Convert.ToDecimal(row.PaymentDetails.Amount),
                            CurrencyCode    = row.PaymentDetails.CurrencyCode,
                            Status          = row.Status
                        };
                        var payment = Mapper.Map <Payment>(paymentDto);
                        paymentRepository.Insert(payment);
                    }
                }
            }
        }
示例#3
0
        static void XmlToJSONSample4()
        {
            using (var parser = new ChoXmlReader <ProductionOrderFile>("sample4.xml").WithXPath("/").Configure(c => c.UseXmlSerialization = true)
                   )
            {
                using (var writer = new ChoJSONWriter("sample3.json").Configure(c => c.UseJSONSerialization = true).Configure(c => c.SupportMultipleContent = false).Configure(c => c.Formatting = Newtonsoft.Json.Formatting.None)
                       )
                    writer.Write(parser);

                //foreach (var x in parser)
                //{
                //    Console.WriteLine(x.ProductionOrderName);
                //    Console.WriteLine("{0}", ((ICollection)x.Batches).Count);
                //    Console.WriteLine("{0}", ((ICollection)x.VariableDatas).Count);
                //}
            }

            //using (var parser = new ChoXmlReader("sample4.xml").WithXPath("/")
            //    .WithField("ProductionOrderName", xPath: "ProductionOrderName")
            //    .WithField("Batches", xPath: "//Batches/Batch", isCollection: true, fieldType: typeof(Batch))
            //    .WithField("VariableDatas", xPath: "//VariableData", isCollection: true, fieldType: typeof(VariableData))
            //    )
            //{
            //    using (var writer = new ChoJSONWriter("sample3.json"))
            //        writer.Write(parser);

            //    //foreach (var x in parser)
            //    //{
            //    //    Console.WriteLine(x.ProductionOrderName);
            //    //    Console.WriteLine("{0}", ((ICollection)x.Batches).Count);
            //    //    Console.WriteLine("{0}", ((ICollection)x.VariableDatas).Count);
            //    //}
            //}
        }
示例#4
0
        static void XmlToCSVSample2()
        {
            using (var parser = new ChoXmlReader("sample2.xml")
                                .WithField("messageID")
                                .WithField("orderNumber")
                                .WithField("model")
                                .WithField("tls")
                                .WithField("status")
                                .WithField("timestamp")
                                .WithField("message")
                                .WithField("attributes", xPath: "attributes/attribute", fieldType: typeof(IList))
                   )
            {
                parser.BeforeRecordFieldLoad += (o, e) =>
                {
                    dynamic a = e.Record;
                    IDictionary <string, object> dict = (IDictionary <string, object>)e.Record;

                    if (e.PropertyName == "attributes")
                    {
                        ((IList <object>)e.Source).Cast <XElement>().Select(e1 =>
                        {
                            dict[e1.Attribute("name").Value] = e1.Attribute("value").Value;

                            return(e1);
                        }).ToArray();

                        e.Skip = true;
                    }
                };
                using (var writer = new ChoCSVWriter("sample2.csv").WithFirstLineHeader())
                    writer.Write(parser);
            }
        }
示例#5
0
 static void LoadTextTest()
 {
     foreach (var x in ChoXmlReader.LoadText(@"<books><book name=""xxx"" author=""Tom""><title>C++</title></book><book name=""yyyy""></book></books>"))
     {
         Console.WriteLine(x.ToStringEx());
     }
 }
示例#6
0
        static void Sample9Test()
        {
            int totalAvailable;

            using (var parser = new ChoXmlReader("sample9.xml", "abc.com/api").WithXPath("/tsResponse/pagination")
                                .WithField("totalAvailable", fieldType: typeof(int))
                                .WithField("pageNumber", fieldType: typeof(int))
                   )
            {
                totalAvailable = parser.FirstOrDefault().totalAvailable;
            }

            using (var parser = new ChoXmlReader("sample9.xml", "abc.com/api").WithXPath("/tsResponse/views/view")
                                .WithField("view_id", xPath: "@id")
                                .WithField("view_name", xPath: "@name")
                                .WithField("view_content_url", xPath: "@contentUrl")
                                .WithField("view_total_count", xPath: "/x:usage/@totalViewCount", fieldType: typeof(int))
                   )
            {
                using (var writer = new ChoJSONWriter("sample9.json")
                       )
                {
                    foreach (dynamic rec in parser)
                    {
                        writer.Write(new { view_id = rec.view_id, view_name = rec.view_name, view_content_url = rec.view_content_url, view_total_count = rec.view_total_count, view_total_available = totalAvailable });
                    }
                    writer.Write(parser);
                }
            }
        }
示例#7
0
        public static void Test1()
        {
            string _xml = @"
<?xml version=""1.0"" encoding=""utf-8"" ?> 
<users>
    <user>
        <userId>1</userId> 
        <firstName>George</firstName> 
        <lastName>Washington</lastName> 
    </user>
    <user>
        <userId>2</userId> 
        <firstName>Abraham</firstName> 
        <lastName>Lincoln</lastName> 
    </user>
    ...
</users>
";

            using (var csvWriter = new ChoCSVWriter(FileNameTest1ActualCSV).WithFirstLineHeader())
            {
                using (var xmlReader = new ChoXmlReader(new StringReader(_xml.Trim())))
                    csvWriter.Write(xmlReader);
            }

            FileAssert.AreEqual(FileNameTest1ExpectedCSV, FileNameTest1ActualCSV);
        }
示例#8
0
 static void Main(string[] args)
 {
     using (var csvWriter = new ChoCSVWriter("users.csv").WithFirstLineHeader())
     {
         using (var xmlReader = new ChoXmlReader(new StringReader(_xml.Trim())))
             csvWriter.Write(xmlReader);
     }
 }
示例#9
0
 private static void Test2()
 {
     using (var csvWriter = new ChoCSVWriter("sample1.csv").WithFirstLineHeader())
     {
         using (var xmlReader = new ChoXmlReader("sample1.xml"))
             csvWriter.Write(xmlReader);
     }
 }
示例#10
0
 private static void Test1()
 {
     using (var csvWriter = new ChoCSVWriter("users.csv").WithFirstLineHeader())
     {
         using (var xmlReader = new ChoXmlReader(new StringReader(_xml.Trim())))
             csvWriter.Write(xmlReader);
     }
 }
示例#11
0
 static void Main(string[] args)
 {
     using (var xmlReader = new ChoXmlReader("Users.xml"))
     {
         using (var csvWriter = new ChoCSVWriter("Users.csv").WithFirstLineHeader().
                                WithField("Id", fieldPosition: 1).WithField("last_name", fieldName: "Name", fieldPosition: 10).ThrowAndStopOnMissingField())
             csvWriter.Write(xmlReader);
     }
 }
示例#12
0
文件: Program.cs 项目: xman086/ChoETL
 public static void XMLToCSVConverterTest()
 {
     using (var xmlReader = new ChoXmlReader("Users.xml"))
     {
         using (var csvWriter = new ChoCSVWriter("Users.csv").WithFirstLineHeader().
                                WithField("Id", fieldPosition: 1).WithField("last_name", fieldName: "Name", fieldPosition: 10).ThrowAndStopOnMissingField())
             csvWriter.Write(xmlReader);
     }
 }
示例#13
0
 static void XmlToCSVSample5()
 {
     using (var parser = new ChoXmlReader("sample5.xml").WithXPath("/PRICE")
            )
     {
         using (var writer = new ChoCSVWriter("sample5.csv").WithFirstLineHeader())
             writer.Write(parser);
     }
 }
示例#14
0
 private static void XmlNullTest()
 {
     using (var parser = new ChoXmlReader("sample13.xml")
            )
     {
         //var c = parser.Select(x => (string)x.AustrittDatum).ToArray();
         using (var jw = new ChoJSONWriter("sample13.json"))
             jw.Write(new { AustrittDatum = parser.Select(x => (string)x.AustrittDatum).ToArray() });
     }
 }
示例#15
0
 static void XmlToCSVSample1()
 {
     using (var parser = new ChoXmlReader("sample.xml").WithXPath("Attributes/Attribute")
                         .WithField("Name", xPath: "Name")
                         .WithField("Value", xPath: "value")
            )
     {
         Console.WriteLine(ChoCSVWriter.ToText(parser.Select(kvp => kvp.Value).ToExpandoObject()));
     }
 }
示例#16
0
        public static void Test2()
        {
            using (var csvWriter = new ChoCSVWriter(FileNameTest2ActualCSV).WithFirstLineHeader())
            {
                using (var xmlReader = new ChoXmlReader(FileNameSample1XML))
                    csvWriter.Write(xmlReader);
            }

            FileAssert.AreEqual(FileNameTest2ExpectedCSV, FileNameTest2ActualCSV);
        }
示例#17
0
        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));
        }
示例#18
0
 public static void Pivot1()
 {
     using (var parser = new ChoXmlReader("pivot1.xml").WithXPath(@"//Values/*")
                         .WithField("Item")
                         .WithField("Value")
            )
     {
         Console.WriteLine(ChoCSVWriter.ToTextAll(parser.Cast <ChoDynamicObject>().Transpose(false),
                                                  new ChoCSVRecordConfiguration().Configure(c => c.FileHeaderConfiguration.HasHeaderRecord = true)));
     }
 }
示例#19
0
 static void Sample6()
 {
     using (var parser = new ChoXmlReader <JobApplication>("sample6.xml")
            )
     {
         foreach (dynamic rec in parser)
         {
             Console.WriteLine(ChoUtility.Dump(rec));
         }
     }
 }
示例#20
0
 public static void Sample15()
 {
     using (var parser = new ChoXmlReader("sample15.xml")
            )
     {
         foreach (dynamic rec in parser)
         {
             Console.WriteLine(ChoUtility.Dump(rec));
         }
     }
 }
示例#21
0
 static void NoEncodeTest()
 {
     using (var xr = new ChoXmlReader("NoEncode.xml")
                     .WithField("id", encodeValue: false)
            )
     {
         foreach (dynamic rec in xr)
         {
             Console.WriteLine(rec.id);
         }
     }
 }
示例#22
0
 static void Sample16()
 {
     using (var parser = new ChoXmlReader("sample16.xml")
            )
     {
         var dict   = parser.ToDictionary(i => (string)i.name, i => (object)i.value, StringComparer.CurrentCultureIgnoreCase);
         var person = dict.ToObject <Person>();
         {
             Console.WriteLine("{0}", person.DateOfBirth);
         }
     }
 }
示例#23
0
 static void Sample12()
 {
     using (var parser = new ChoXmlReader("sample12.xml")
                         .WithField("SelectedIdValue", xPath: "//SelectedIds", fieldType: typeof(SelectedIds))
            )
     {
         foreach (dynamic rec in parser)
         {
             Console.WriteLine("{0}", rec.GetXml());
         }
     }
 }
示例#24
0
 static void Sample11Test()
 {
     using (var parser = new ChoXmlReader("sample11.xml").WithXPath("/members/father")
                         .WithField("id")
                         .WithField("sons")
            )
     {
         foreach (dynamic rec in parser)
         {
             Console.WriteLine(ChoUtility.DumpAsJson(rec.sons));
         }
     }
 }
示例#25
0
 static void XmlToCSVSample7()
 {
     using (var parser = new ChoXmlReader("sample7.xml").WithXPath("/UpdateDB/Transaction")
                         .WithField("Table", xPath: "/Insert/Table")
                         .WithField("szCustomerID", xPath: "/Insert/Set/szCustomerID")
                         .WithField("szCustomerName", xPath: "/Insert/Set/szCustomerName")
                         .WithField("szExternalID", xPath: "/Insert/Set/szExternalID")
            )
     {
         using (var writer = new ChoCSVWriter("sample7.csv").WithFirstLineHeader())
             writer.Write(parser.Where(r => r.Table == "CUSTOMER").Select(r => new { szCustomerID = r.szCustomerID, szCustomerName = r.szCustomerName, szExternalID = r.szExternalID }));
     }
 }
示例#26
0
 static void Sample8Test()
 {
     using (var parser = new ChoXmlReader("sample8.xml").WithXPath("/root/data")
                         .WithField("id", xPath: "@name")
                         .WithField("text", xPath: "/value")
            )
     {
         using (var writer = new ChoJSONWriter("sample8.json")
                             .Configure(c => c.SupportMultipleContent = true)
                )
             writer.Write(new { Texts = parser.ToArray() });
     }
 }
示例#27
0
        static void QuickTestWithXmlNS()
        {
            using (var stream = new MemoryStream())
                using (var reader = new StreamReader(stream))
                    using (var writer = new StreamWriter(stream))
                        using (var parser = new ChoXmlReader(reader).WithXPath("/cont:contacts/cont:contact/cont:contact1").WithXmlNamespace("cont", "www.tutorialspoint.com/profile").WithField("name", "cont:name"))
                        {
                            writer.WriteLine(@"<cont:contacts xmlns:cont=""www.tutorialspoint.com/profile"">
                <cont:contact >
                    <cont:contact1 >
                       <cont:name>Tanmay Patilx</cont:name>
                       <cont:company>TutorialsPoint</cont:company>
                       <cont:phone> (011) 123 - 4567 </cont:phone>
                    </cont:contact1> 
                    <cont:contact1 >
                       <cont:name>Tanmay Patilx1</cont:name>
                       <cont:company>TutorialsPoint</cont:company>
                       <cont:phone> (011) 123 - 4567 </cont:phone>
                    </cont:contact1> 
                   <cont:name>Tanmay Patil</cont:name>
                   <cont:company>TutorialsPoint</cont:company>
                   <cont:phone> (011) 123 - 4567 </cont:phone>
                </cont:contact> 
                <cont:contact >
                     <cont:contact1 >
                       <cont:name>Tanmay Patily</cont:name>
                       <cont:company>TutorialsPoint</cont:company>
                       <cont:phone> (011) 123 - 4567 </cont:phone>
                    </cont:contact1> 
                      <cont:contact1 >
                       <cont:name>Tanmay Patily1</cont:name>
                       <cont:company>TutorialsPoint</cont:company>
                       <cont:phone> (011) 123 - 4567 </cont:phone>
                    </cont:contact1> 
                 <cont:name>Tanmay Patil1</cont:name>
                   <cont:company>TutorialsPoint1</cont:company>
                   <cont:phone> (011) 123 - 45671 </cont:phone>
                </cont:contact> 
                </cont:contacts>
                ");

                            writer.Flush();
                            stream.Position = 0;

                            object rec;
                            while ((rec = parser.Read()) != null)
                            {
                                Console.WriteLine(rec.ToStringEx());
                            }
                        }
        }
示例#28
0
        public static void NullableTest()
        {
            string xml = @"<?xml version=""1.0""?>
    <Item Number = ""100"" ItemName = ""TestName1"" ItemId = ""1"" />";

            XDocument doc = XDocument.Parse(xml);

            var item = ChoXmlReader <Item> .LoadXElements(new XElement[] { doc.Root }).FirstOrDefault();

            Console.WriteLine($"ItemId: {item.ItemId}");
            Console.WriteLine($"ItemName: {item.ItemName}");
            Console.WriteLine($"Number: {item.Number}");
            Console.WriteLine($"Created: {item.Created}");
        }
示例#29
0
 static void Sample17()
 {
     using (var xr = new ChoXmlReader("Sample17.xml").WithXPath("//HouseInfo")
                     .WithField("HouseNumber", fieldType: typeof(int))
                     .WithField("RoomInfos", xPath: "//HouseLog/RoomInfo", fieldType: typeof(List <RoomInfo>))
                     .WithField("Furnitures", xPath: "//HouseLog/RoomInfo/Furnitures/Table", fieldType: typeof(Table))
            )
     {
         foreach (dynamic rec in xr)
         {
             Console.WriteLine(rec.Dump());
         }
     }
 }
示例#30
0
 //[Test]
 public static void XMLToCSVConverterTest()
 {
     using (var xmlReader = new ChoXmlReader("Users.xml")
                            .WithXPath("users/user")
            )
     {
         //foreach (var rec in xmlReader)
         //    Console.WriteLine(rec.Dump());
         //return;
         using (var csvWriter = new ChoCSVWriter("Users.csv").WithFirstLineHeader()
                                .Configure(c => c.UseNestedKeyFormat = false)
                                .WithField("Id").WithField("last_name", fieldName: "Name").ThrowAndStopOnMissingField())
             csvWriter.Write(xmlReader);
     }
 }