Exemplo n.º 1
0
        private static void CreateTransform(string sourcefile, string targetfile, string transfile)
        {
            List <TransformElement> transformElements = new List <TransformElement>();

            XDocument xsource, xtarget;

            try
            {
                xsource = XDocument.Load(sourcefile);
            }
            catch (IOException ex)
            {
                Console.WriteLine(ex.Message);
                return;
            }
            try
            {
                xtarget = XDocument.Load(targetfile);
            }
            catch (IOException ex)
            {
                Console.WriteLine(ex.Message);
                return;
            }

            XElement[] elements = xsource
                                  .Descendants()
                                  .Concat(xtarget.Descendants())
                                  .ToArray();

            XElement[] sourceElements = xsource
                                        .Descendants()
                                        .ToArray();

            XElement[] targetElements = xtarget
                                        .Descendants()
                                        .ToArray();

            Console.WriteLine($"Total elements: {elements.Length} ({sourceElements.Length} + {targetElements.Length})");

            string[] elementpaths = elements
                                    .Select(e => XmlHelper.GetElementPath(e))
                                    .Distinct()
                                    .OrderBy(p => p)
                                    .ToArray();

            Console.WriteLine($"Distinct paths: {elementpaths.Length}");

            foreach (string elementpath in elementpaths)
            {
                sourceElements = xsource
                                 .Descendants()
                                 .Where(el => XmlHelper.GetElementPath(el) == elementpath)
                                 .ToArray();

                targetElements = xtarget
                                 .Descendants()
                                 .Where(el => XmlHelper.GetElementPath(el) == elementpath)
                                 .ToArray();

                foreach (XElement targetel in targetElements)
                {
                    int b = 123;
                    if (targetel.Name.LocalName == "add" && targetel.Attribute("name") != null && targetel.Attribute("name").Value == "membershipProvider")
                    {
                        b++;
                    }

                    // If exist, update. Else add.

                    XElement[] matchingElements = sourceElements
                                                  .Where(sourceel => XmlHelper.AreEqualId(targetel, sourceel))
                                                  .ToArray();
                    if (matchingElements.Length > 1)
                    {
                        // Dont know which sourceel targetel match.
                        Console.WriteLine($"Ignoring element: {elementpath}, matching too many elements: {matchingElements.Length}");
                        continue;
                    }
                    else if (matchingElements.Length == 1)
                    {
                        if (!XmlHelper.AreEqual(targetel, matchingElements[0]))
                        {
                            //Console.WriteLine($"Updating: {elementpath}");
                            Console.Write("u");
                            TransformElement updateel = new TransformElement
                            {
                                op       = Operation.update,
                                Path     = elementpath,
                                Xelement = new XElement(targetel.Name.LocalName, targetel.Attributes())
                            };
                            transformElements.Add(updateel);
                        }
                    }
                    else
                    {
                        if (!sourceElements.Any(sourceel => XmlHelper.AreEqual(sourceel, targetel)))
                        {
                            //Console.WriteLine($"Insert: elementpath}");
                            Console.Write("i");
                            TransformElement insertel = new TransformElement
                            {
                                op       = Operation.insert,
                                Path     = elementpath,
                                Xelement = new XElement(targetel.Name.LocalName, targetel.Attributes())
                            };
                            transformElements.Add(insertel);
                        }
                    }
                }

                foreach (XElement sourceel in sourceElements)
                {
                    if (!targetElements.Any(targetel => XmlHelper.AreEqualId(sourceel, targetel)))
                    {
                        Console.Write("d");
                        //Console.WriteLine($"Delete: {elementpath}");
                        TransformElement removeel = new TransformElement
                        {
                            op       = Operation.remove,
                            Path     = elementpath,
                            Xelement = new XElement(sourceel.Name.LocalName,
                                                    sourceel.Attributes().Where(a => XmlHelper.idattributenames.Contains(a.Name.LocalName)).OrderBy(a => a))
                        };
                        transformElements.Add(removeel);
                    }
                }
            }


            WriteElements(transformElements, transfile);
        }
Exemplo n.º 2
0
        private static void CreateTransform(string sourcefile, string targetfile, string transfile)
        {
            List<TransformElement> transformElements = new List<TransformElement>();

            XDocument xsource, xtarget;

            try
            {
                xsource = XDocument.Load(sourcefile);
            }
            catch (IOException ex)
            {
                Console.WriteLine(ex.Message);
                return;
            }
            try
            {
                xtarget = XDocument.Load(targetfile);
            }
            catch (IOException ex)
            {
                Console.WriteLine(ex.Message);
                return;
            }

            XElement[] elements = xsource
                .Descendants()
                .Concat(xtarget.Descendants())
                .ToArray();

            XElement[] sourceElements = xsource
                .Descendants()
                .ToArray();

            XElement[] targetElements = xtarget
                .Descendants()
                .ToArray();

            Console.WriteLine("Total elements: " + elements.Length + " (" + sourceElements.Length + " + " + targetElements.Length + ")");

            string[] elementpaths = elements
                .Select(e => XmlHelper.GetElementPath(e))
                .Distinct()
                .OrderBy(p => p)
                .ToArray();

            Console.WriteLine("Distinct paths: " + elementpaths.Length);

            foreach (string elementpath in elementpaths)
            {
                sourceElements = xsource
                    .Descendants()
                    .Where(el => XmlHelper.GetElementPath(el) == elementpath)
                    .ToArray();

                targetElements = xtarget
                    .Descendants()
                    .Where(el => XmlHelper.GetElementPath(el) == elementpath)
                    .ToArray();

                foreach (XElement targetel in targetElements)
                {
                    int b = 123;
                    if (targetel.Name.LocalName == "add" && targetel.Attribute("name") != null && targetel.Attribute("name").Value == "membershipProvider")
                    {
                        b++;
                    }

                    // If exist, update. Else add.

                    XElement[] matchingElements = sourceElements
                        .Where(sourceel => XmlHelper.AreEqualId(targetel, sourceel))
                        .ToArray();
                    if (matchingElements.Length > 1)
                    {
                        // Dont know which sourceel targetel match.
                        Console.WriteLine("Ignoring element: " + elementpath + ", matching too many elements: " + matchingElements.Length);
                        continue;
                    }
                    else if (matchingElements.Length == 1)
                    {
                        if (!XmlHelper.AreEqual(targetel, matchingElements[0]))
                        {
                            //Console.WriteLine("Updating: " + elementpath);
                            Console.Write("u");
                            TransformElement updateel = new TransformElement();
                            updateel.op = operation.update;
                            updateel.path = elementpath;
                            updateel.xelement = new XElement(targetel.Name.LocalName, targetel.Attributes());
                            transformElements.Add(updateel);
                        }
                    }
                    else
                    {
                        if (!sourceElements.Any(sourceel => XmlHelper.AreEqual(sourceel, targetel)))
                        {
                            //Console.WriteLine("Insert: " + elementpath);
                            Console.Write("i");
                            TransformElement insertel = new TransformElement();
                            insertel.op = operation.insert;
                            insertel.path = elementpath;
                            insertel.xelement = new XElement(targetel.Name.LocalName, targetel.Attributes());
                            transformElements.Add(insertel);
                        }
                    }
                }

                foreach (XElement sourceel in sourceElements)
                {
                    if (!targetElements.Any(targetel => XmlHelper.AreEqualId(sourceel, targetel)))
                    {
                        Console.Write("d");
                        //Console.WriteLine("Delete: " + elementpath);
                        TransformElement removeel = new TransformElement();
                        removeel.op = operation.remove;
                        removeel.path = elementpath;
                        removeel.xelement = new XElement(sourceel.Name.LocalName,
                            sourceel.Attributes().Where(a => XmlHelper.idattributenames.Contains(a.Name.LocalName)).OrderBy(a => a));
                        transformElements.Add(removeel);
                    }
                }
            }

            WriteElements(transformElements, transfile);
        }