コード例 #1
0
        public static void DivideOrders(String xmlPath)
        {
            int numberOfDividedFiles = 0;

            Employee[] employees = new Employee[3];
            employees[0]          = new Employee();
            employees[0].name     = "employee2";
            employees[0].products = new string[] { "1" };
            employees[1]          = new Employee();
            employees[1].name     = "employee3";
            employees[1].products = new string[] { "2" };
            employees[2]          = new Employee();
            employees[2].name     = "employee4";
            employees[2].products = new string[] { "3" };

            List <XmlNode> products = new List <XmlNode>();
            XmlDocument    xmlDoc   = new XmlDocument();

            xmlDoc.Load(xmlPath);

            XmlReader reader = new XmlNodeReader(xmlDoc);
            DataSet   newDs  = new DataSet();

            newDs.ReadXml(reader);
            DataTable dt = newDs.Tables[0];
            DataRelationCollection children = dt.ChildRelations;

            for (int i = 0; i < xmlDoc.DocumentElement.ChildNodes[0].ChildNodes.Count; i++)
            {
                String x = xmlDoc.DocumentElement.ChildNodes[0].ChildNodes[i].Name;
                if (xmlDoc.DocumentElement.ChildNodes[0].ChildNodes[i].Name == "product")
                {
                    products.Add(xmlDoc.DocumentElement.ChildNodes[0].ChildNodes[i]);
                }
            }

            for (int i = 0; i < employees.Length; i++)
            {
                bool        ifAnyProductMatchEmployee = false;
                XmlDocument xmlDocdest = new XmlDocument();

                XmlNode rootNode = xmlDocdest.CreateElement("orders");
                xmlDocdest.AppendChild(rootNode);

                XmlNode orderNode = xmlDocdest.CreateElement("order");
                rootNode.AppendChild(orderNode);

                AddOrderInformation(xmlDocdest,
                                    orderNode,
                                    xmlDoc.DocumentElement.ChildNodes[0].ChildNodes[0].InnerText,
                                    xmlDoc.DocumentElement.ChildNodes[0].ChildNodes[1].InnerText,
                                    xmlDoc.DocumentElement.ChildNodes[0].ChildNodes[2].ChildNodes[0].InnerText,
                                    xmlDoc.DocumentElement.ChildNodes[0].ChildNodes[2].ChildNodes[1].InnerText,
                                    xmlDoc.DocumentElement.ChildNodes[0].ChildNodes[2].ChildNodes[2].InnerText);


                foreach (XmlNode product in products)
                {
                    if (employees[i].products.Contains(product.ChildNodes[0].InnerText))
                    {
                        AddProduct(xmlDocdest, orderNode, product);
                        ifAnyProductMatchEmployee = true;
                    }
                }
                if (ifAnyProductMatchEmployee)
                {
                    int    employeeNumber = i + 2;
                    String pathToXml      = "..\\..\\xmls_result\\divided_xml_employee_" + employeeNumber + ".xml";
                    String pathToExcel    = "..\\..\\excel_result\\divided_excel_employee_" + employeeNumber + ".xlsx";
                    xmlDocdest.Save(pathToXml);
                    ExcelManager.XmlToExcel(pathToXml, pathToExcel);
                    Mail mail = new Mail();
                    mail.send(employeeNumber, pathToExcel, 1);
                    numberOfDividedFiles++;
                }
            }
        }