Пример #1
0
        static void Main(string[] args)
        {
            Serializer ser           = new Serializer();
            string     path          = string.Empty;
            string     xmlInputData  = string.Empty;
            string     xmlOutputData = string.Empty;

            path         = Directory.GetCurrentDirectory() + @"\Salida.xml";
            xmlInputData = File.ReadAllText(path);

            Facturas factura = ser.Deserialize <Facturas>(xmlInputData);

            xmlOutputData = ser.Serialize <Facturas>(factura);

            List <PropiedadesFactura> listaFact = new List <PropiedadesFactura>();
            Concepto concepto = new Concepto();

            for (int i = 0; i < factura.Propiedades.Count; i++)
            {
                listaFact.Add(factura.Propiedades[i]);
            }

            IEnumerable <PropiedadesFactura> distint2 = listaFact.GroupBy(x => x.FolioABA).Select(y => y.First()).Where(element => element.FolioABA != "0").Distinct();

            IEnumerable <PropiedadesFactura> distint3 = listaFact.Select(
                element => element).Where(
                element => element.IdGeneral == 4);

            List <XmlToObjectPrueba01.ServiceReference1.Concepto> IdConceptos =
                new List <XmlToObjectPrueba01.ServiceReference1.Concepto>();

            foreach (PropiedadesFactura facturas in distint2)
            {
                Console.WriteLine(facturas.FolioABA);

                IEnumerable <PropiedadesFactura> distint4 = listaFact.Select(
                    element => element).Where(
                    element => element.IdGeneral == 4 && element.FolioABA == facturas.FolioABA);

                IEnumerable <PropiedadesFactura> distint5 = distint4.GroupBy(x => x.IdConcepto)
                                                            .Select(y => y.First()).Distinct();

                foreach (PropiedadesFactura createObject in distint5)
                {
                    IEnumerable <PropiedadesFactura> objectCreated = listaFact
                                                                     .Where(e => e.FolioABA == facturas.FolioABA &&
                                                                            e.IdConcepto == createObject.IdConcepto);

                    XmlToObjectPrueba01.ServiceReference1.Concepto ConceptoClassWCF =
                        new XmlToObjectPrueba01.ServiceReference1.Concepto();

                    //object[,] prueba = new object[objectCreated.Count(), objectCreated.Count()];
                    //int i = 0;
                    //foreach (var Datos in objectCreated)
                    //{
                    //    prueba[i,0] = Datos.Valor;
                    //    prueba[0,i] = Datos.Dato;
                    //    i++;
                    //}
                    //i = 0;
                    var dictionary = new Dictionary <String, object>();
                    //int i = 0;
                    foreach (var Datos in objectCreated)
                    {
                        Regex regex = new Regex(@"\w*$");
                        Match match = regex.Match(Datos.Valor);
                        dictionary.Add(match.ToString(), Datos.Dato);
                        //Console.WriteLine(dictionary[i]);
                        //i++;
                    }
                    //i = 0;

                    PropertyInfo[] properties = typeof(XmlToObjectPrueba01.ServiceReference1.Concepto)
                                                .GetProperties();
                    int x = 0;


                    foreach (PropertyInfo proper in properties)
                    {
                        if (proper.PropertyType == typeof(String) ||
                            proper.PropertyType == typeof(int))

                        {
                            if (dictionary.ContainsKey(proper.Name.ToString()))
                            {
                                //object values = proper.Name;
                                //Console.WriteLine(dictionary[proper.Name.ToString()]);
                                proper.SetValue(ConceptoClassWCF, dictionary[proper.Name.ToString()]);
                            }
                        }
                        else
                        if (proper.PropertyType == typeof(Decimal))
                        {
                            if (dictionary.ContainsKey(proper.Name.ToString()))
                            {
                                //object values = proper.Name;
                                //Console.WriteLine(dictionary[proper.Name.ToString()]);
                                proper.SetValue(ConceptoClassWCF, Convert.ToDecimal(dictionary[proper.Name.ToString()]));
                            }
                        }
                    }

                    IdConceptos.Add(ConceptoClassWCF);
                }
            }
            Console.ReadLine();
        }
        static void Main(string[] args)
        {
            // Initialize custom class for xml serialization
            Serializer ser           = new Serializer();
            string     path          = string.Empty;
            string     xmlInputData  = string.Empty;
            string     xmlOutputData = string.Empty;

            // Declare the path for xml file
            path         = Directory.GetCurrentDirectory() + @"\Salida.xml";
            xmlInputData = File.ReadAllText(path);

            // Initialize the Class for match xml node with name of class
            Facturas factura = ser.Deserialize <Facturas>(xmlInputData);

            // Initialize listaFactura Object
            List <PropiedadesFactura> listaFact = new List <PropiedadesFactura>();

            // Fill the object with values form xml
            for (int i = 0; i < factura.Propiedades.Count; i++)
            {
                listaFact.Add(factura.Propiedades[i]);
            }

            // Object's list from facturas
            IEnumerable <PropiedadesFactura> filter1 = listaFact.GroupBy(x => x.FolioABA)
                                                       .Select(y => y.First()).Where(element => element.FolioABA != "0");

            // Object's list from Concepto
            //List<XmlToObjectPrueba01.ServiceReference1.Concepto> IdConceptos =
            //    new List<XmlToObjectPrueba01.ServiceReference1.Concepto>();

            //Object's list from Concepto
            //List<ConceptoClassWCF> IdConceptos =
            //    new List<ConceptoClassWCF>();
            List <object> IdConceptos = new List <object>();

            //const string objectToInstantiate2 =
            //            "XmlToObjectPrueba01.ServiceReference1.Concepto, XmlToObjectPrueba01";
            //var ConceptoClass2 = Type.GetType(objectToInstantiate2);
            //var ConceptoClassWCF = Activator.CreateInstance(ConceptoClass2);

            //List<ConceptoClass2> c = new List<ConceptoClass2>();

            #region Iteration 1 - Facturas
            foreach (PropiedadesFactura facturas in filter1)
            {
                // Object's list from IdGeneral
                IEnumerable <PropiedadesFactura> IGIL = listaFact.GroupBy(e => e.IdGeneral)
                                                        .Select(y => y.First()).Where(e => e.FolioABA == facturas.FolioABA);

                #region Iteration 2 - IdGeneral
                foreach (PropiedadesFactura IdGenerales in IGIL)
                {
                    // Object's list from IdGeneral
                    IEnumerable <PropiedadesFactura> filter2 = listaFact.Select(
                        element => element).Where(
                        element => element.IdGeneral == IdGenerales.IdGeneral && element.FolioABA == facturas.FolioABA);

                    // Object's list from ValorUnitario - Class Name
                    IEnumerable <PropiedadesFactura> CNL = listaFact.Select(
                        element => element).Where(
                        element => element.IdGeneral ==
                        IdGenerales.IdGeneral && element.FolioABA == facturas.FolioABA);

                    // Add Filter for value unique in the class name for every ClassName

                    var ClasesName = new Dictionary <String, object>();

                    // Fill the Dictionary with data - Key = Valor && Value = Dato
                    foreach (var Datos in CNL)
                    {
                        // Regular expresion by get all characters after dot
                        Regex regex = new Regex(@"^\w*");
                        // Get data for Regular expression
                        Match match = regex.Match(Datos.Valor);
                        // Add Keys and Values
                        ClasesName.Add(match.ToString(), Datos.Dato);
                    }

                    //// Object's list from IdGeneral
                    //IEnumerable<PropiedadesFactura> filter2 = listaFact.Select(
                    //   element => element).Where(
                    //   element => element.IdGeneral == 4 && element.FolioABA == facturas.FolioABA);

                    // Object's list from IdConcepto
                    IEnumerable <PropiedadesFactura> filter3 = filter2.GroupBy(x => x.IdConcepto)
                                                               .Select(y => y.First()).Distinct();
                    #region Iteration 2 - IdConcepto inside each Factura
                    foreach (PropiedadesFactura createObject in filter3)
                    {
                        // Object's list from each IdConcepto inside IdGeneral
                        IEnumerable <PropiedadesFactura> objectCreated = listaFact
                                                                         .Where(e => e.FolioABA == facturas.FolioABA &&
                                                                                e.IdConcepto == createObject.IdConcepto);

                        // Initialize class Concepto from ServicesReference
                        //XmlToObjectPrueba01.ServiceReference1.Concepto ConceptoClassWCF =
                        //new XmlToObjectPrueba01.ServiceReference1.Concepto();
                        const string objectToInstantiate =
                            "XmlToObjectPrueba01.ServiceReference1." + "Concepto" + "," + " XmlToObjectPrueba01";
                        var ConceptoClass    = Type.GetType(objectToInstantiate);
                        var ConceptoClassWCF = Activator.CreateInstance(ConceptoClass);

                        //ConceptoClass = Type class;

                        // Initialize Dictoriony by object's list from data - Concepto
                        var dictionary = new Dictionary <String, object>();

                        // Fill the Dictionary with data - Key = Valor && Value = Dato
                        foreach (var Datos in objectCreated)
                        {
                            // Regular expresion by get all characters after dot
                            Regex regex = new Regex(@"\w*$");
                            // Get data for Regular expression
                            Match match = regex.Match(Datos.Valor);
                            // Add Keys and Values
                            dictionary.Add(match.ToString(), Datos.Dato);
                        }

                        // Get array properties from Class Concepto - Service Reference
                        PropertyInfo[] properties = typeof(XmlToObjectPrueba01.ServiceReference1.Concepto)
                                                    .GetProperties();

                        #region Iteration 3 - Set values to Concepto - Services Reference
                        foreach (PropertyInfo proper in properties)
                        {
                            // Filters for properties from object Conceptos - Service Reference
                            if (proper.PropertyType == typeof(String))
                            {
                                if (dictionary.ContainsKey(proper.Name.ToString()))
                                {
                                    proper.SetValue(ConceptoClassWCF,
                                                    dictionary[proper.Name.ToString()]);
                                }
                            }
                            else if (proper.PropertyType == typeof(Decimal))
                            {
                                if (dictionary.ContainsKey(proper.Name.ToString()))
                                {
                                    proper.SetValue(ConceptoClassWCF,
                                                    Convert.ToDecimal(dictionary[proper.Name.ToString()]));
                                }
                            }
                            else if (proper.PropertyType == typeof(int))
                            {
                                if (dictionary.ContainsKey(proper.Name.ToString()))
                                {
                                    proper.SetValue(ConceptoClassWCF,
                                                    Convert.ToInt32(dictionary[proper.Name.ToString()]));
                                }
                            }
                        }
                        #endregion
                        // Add object Concepto to List<Concepto>
                        IdConceptos.Add(ConceptoClassWCF);
                    }
                    #endregion
                }
                #endregion
            }
            #endregion
            Console.ReadLine();
        }
        static void Main(string[] args)
        {
            #region Variables - Xml Transformation

            // Initialize custom class for xml serialization
            Serializer ser           = new Serializer();
            string     path          = string.Empty;
            string     xmlInputData  = string.Empty;
            string     xmlOutputData = string.Empty;

            // Declare the path for xml file
            path         = Directory.GetCurrentDirectory() + @"\Salida.xml";
            xmlInputData = File.ReadAllText(path);

            // Initialize the Class for match xml node with name of class
            Facturas factura = ser.Deserialize <Facturas>(xmlInputData);

            // Initialize listaFactura Object
            List <PropiedadesFactura> listaFact = new List <PropiedadesFactura>();

            //XmlToObjectPrueba01.ServiceReference1.Concepto prueba = new XmlToObjectPrueba01.ServiceReference1.Concepto();
            //prueba.Impuestos.Traslados.TrasladoLista[0].

            #region Loop 1 - Add facturas a la lista
            // Fill the object with values form xml
            for (int i = 0; i < factura.Propiedades.Count; i++)
            {
                listaFact.Add(factura.Propiedades[i]);
            }
            #endregion END - Loop 1

            // Object's list from facturas
            IEnumerable <PropiedadesFactura> filterFacturas1 = listaFact.GroupBy(x => x.FolioABA)
                                                               .Select(y => y.First()).Where(e => e.FolioABA != "0");

            // Object's list from Clases in Service
            List <object> IdConceptos = new List <object>();

            #endregion END Variables
            #region Iteration 1 - Get All Facturas from Xml
            foreach (PropiedadesFactura facturas in filterFacturas1)
            {
                // Object's list from IdGeneral
                IEnumerable <PropiedadesFactura> propertyClassGeneric = listaFact.GroupBy(e => e.IdGeneral)
                                                                        .Select(y => y.First()).Where(e => e.FolioABA == facturas.FolioABA);

                #endregion
                #region Iteration 2 - Get All IdGeneral for each Factura in Loop
                foreach (PropiedadesFactura IdGenerales in propertyClassGeneric)
                {
                    // Object's list from IdGeneral in each FolioABA
                    IEnumerable <PropiedadesFactura> filterIIdGeneral2 = listaFact.Select(
                        e => e).Where(
                        e => e.IdGeneral == IdGenerales.IdGeneral &&
                        e.FolioABA == facturas.FolioABA);

                    // Object's list from ValorUnitario - Class Name
                    IEnumerable <PropiedadesFactura> ClaseCampoValor = listaFact.Select(
                        e => e).Where(
                        e => e.IdGeneral == IdGenerales.IdGeneral &&
                        e.FolioABA == facturas.FolioABA);

                    // Initialize Dictionary and List
                    var           ClasesName  = new Dictionary <String, object>();
                    List <string> arrayClases = new List <string>();

                    // Fill the Dictionary with data - Key = Valor && Value = Dato
                    foreach (var Datos in ClaseCampoValor)
                    {
                        // Regular expresion by get all characters before dot
                        Regex regex = new Regex(@"^\w*");
                        // Get data for Regular expression
                        Match match = regex.Match(Datos.Valor);

                        if (ClasesName.ContainsKey(match.ToString()))
                        {
                            // Do nothing when the key is repeat
                        }
                        else
                        {
                            // Add Variables to Dictionary and Array
                            arrayClases.Add(match.ToString());
                            ClasesName.Add(match.ToString(), Datos.Dato);
                        }
                    }

                    // Object's list from IdConcepto
                    IEnumerable <PropiedadesFactura> filterIdConcepto3 = filterIIdGeneral2.GroupBy(x => x.IdConcepto)
                                                                         .Select(y => y.First()).Distinct();
                    #endregion
                    #region BEGIN Iteration 3 - Get All IdConcepto for each IdGeneral in Loop
                    foreach (PropiedadesFactura createObject in filterIdConcepto3)
                    {
                        // Object's list from each IdConcepto in each IdGeneral in each Factura
                        IEnumerable <PropiedadesFactura> objectCreated = filterIIdGeneral2
                                                                         .Where(e => e.FolioABA == facturas.FolioABA &&
                                                                                e.IdConcepto == createObject.IdConcepto);

                        string cl    = arrayClases[0].ToString();
                        string clase = cl.Substring(0, 1).ToUpper() + cl.Substring(1);


                        if (clase == "Addenda")
                        {
                            IEnumerable <PropiedadesFactura> AddendaList = listaFact.Select(
                                e => e).Where(
                                e => e.FolioABA == facturas.FolioABA && e.Addenda != "0");

                            //public static String Addeenda;


                            foreach (var Adenda in AddendaList)
                            {
                                Variables.Addenda = Adenda.Addenda;
                            }
                        }


                        string objectToInstantiate =
                            "XmlToObjectPrueba01.ServiceReference1." + clase + "," + " XmlToObjectPrueba01";
                        var CopyClass = Type.GetType(objectToInstantiate);



                        if (CopyClass == null)
                        {
                            continue;
                        }

                        //if (CopyClass == "Concepto")
                        //{
                        //    Console.WriteLine(CopyClass);
                        //}

                        var GenericInstace = Activator.CreateInstance(CopyClass);
                        // Initialize Dictoriony by object's list from data - Concepto
                        var dictionary = new Dictionary <String, object>();

                        #region Create List of objects
                        // Fill the Dictionary with data - Key = Valor && Value = Dato
                        foreach (var Datos in objectCreated)
                        {
                            // Regular expresion by get all characters after dot
                            Regex regex = new Regex(@"\w*$");
                            // Get data for Regular expression
                            Match match = regex.Match(Datos.Valor);
                            // Add Keys and Values
                            dictionary.Add(match.ToString(), Datos.Dato);
                        }
                        #endregion
                        // Get array properties from Class Concepto - Service Reference
                        //PropertyInfo[] properties = typeof(XmlToOb jectPrueba01.ServiceReference1.Concepto)
                        //    .GetProperties();

                        // Get array properties from Class Concepto - Service Reference
                        PropertyInfo[] properties = Type.GetType(objectToInstantiate).GetProperties();
                        #endregion END Iteration 4
                        #region Iteration 4 - Set values to Class - Services Reference

                        foreach (PropertyInfo proper in properties)
                        {
                            #region BEGIN - Conditions to Insert values in class
                            // Filters for properties from object Conceptos - Service Reference

                            if (proper.Name == "Addenda")
                            {
                                proper.SetValue(GenericInstace, Variables.Addenda);
                            }
                            else if (proper.Name == "Impuestos")
                            {
                                Console.WriteLine(proper);
                                // LLAMAR A LA PROPIEDAD DE LA CLASE DENTRO
                            }
                            else if (proper.PropertyType == typeof(String[]))
                            {
                                if (dictionary.ContainsKey(proper.Name.ToString()))
                                {
                                    proper.SetValue(GenericInstace,
                                                    new String[] { dictionary[proper.Name].ToString() });
                                }
                            }
                            else if (proper.PropertyType == typeof(String))
                            {
                                if (dictionary.ContainsKey(proper.Name.ToString()))
                                {
                                    proper.SetValue(GenericInstace,
                                                    dictionary[proper.Name.ToString()]);
                                }
                            }
                            else if (proper.PropertyType == typeof(Decimal))
                            {
                                if (dictionary.ContainsKey(proper.Name.ToString()))
                                {
                                    proper.SetValue(GenericInstace,
                                                    Convert.ToDecimal(dictionary[proper.Name.ToString()]));
                                }
                            }
                            else if (proper.PropertyType == typeof(int))
                            {
                                if (dictionary.ContainsKey(proper.Name.ToString()))
                                {
                                    proper.SetValue(GenericInstace,
                                                    Convert.ToInt32(dictionary[proper.Name.ToString()]));
                                }
                                // switch (t.PropertyType.FullName)
                            }
                            #endregion END - Conditions for set values
                        }
                        #endregion END Iteration 4
                        // Add Object to object ListFact
                        IdConceptos.Add(GenericInstace);
                    }
                }
            }
            Console.ReadLine();
        }