Novacode.DocX.merge_numbering C# (CSharp) Method

merge_numbering() private method

private merge_numbering ( PackagePart remote_pp, PackagePart local_pp, System.Xml.Linq.XDocument remote_mainDoc, DocX remote ) : void
remote_pp System.IO.Packaging.PackagePart
local_pp System.IO.Packaging.PackagePart
remote_mainDoc System.Xml.Linq.XDocument
remote DocX
return void
        private void merge_numbering(PackagePart remote_pp, PackagePart local_pp, XDocument remote_mainDoc, DocX remote)
        {
            // Add each remote numbering to this document.
            IEnumerable<XElement> remote_abstractNums = remote.numbering.Root.Elements(XName.Get("abstractNum", w.NamespaceName));
            int guidd = 0;
            foreach (var an in remote_abstractNums)
            {
                XAttribute a = an.Attribute(XName.Get("abstractNumId", w.NamespaceName));
                if (a != null)
                {
                    int i;
                    if (int.TryParse(a.Value, out i))
                    {
                        if (i > guidd)
                            guidd = i;
                    }
                }
            }
            guidd++;

            IEnumerable<XElement> remote_nums = remote.numbering.Root.Elements(XName.Get("num", w.NamespaceName));
            int guidd2 = 0;
            foreach (var an in remote_nums)
            {
                XAttribute a = an.Attribute(XName.Get("numId", w.NamespaceName));
                if (a != null)
                {
                    int i;
                    if (int.TryParse(a.Value, out i))
                    {
                        if (i > guidd2)
                            guidd2 = i;
                    }
                }
            }
            guidd2++;

            foreach (XElement remote_abstractNum in remote_abstractNums)
            {
                XAttribute abstractNumId = remote_abstractNum.Attribute(XName.Get("abstractNumId", w.NamespaceName));
                if (abstractNumId != null)
                {
                    String abstractNumIdValue = abstractNumId.Value;
                    abstractNumId.SetValue(guidd);

                    foreach (XElement remote_num in remote_nums)
                    {
                        var numIds = remote_mainDoc.Descendants(XName.Get("numId", w.NamespaceName));
                        foreach (var numId in numIds)
                        {
                            XAttribute attr = numId.Attribute(XName.Get("val", w.NamespaceName));
                            if (attr != null && attr.Value.Equals(remote_num.Attribute(XName.Get("numId", w.NamespaceName)).Value))
                            {
                                attr.SetValue(guidd2);
                            }

                        }
                        remote_num.SetAttributeValue(XName.Get("numId", w.NamespaceName), guidd2);

                        XElement e = remote_num.Element(XName.Get("abstractNumId", w.NamespaceName));
                        XAttribute a2 = e?.Attribute(XName.Get("val", w.NamespaceName));
                        if (a2 != null && a2.Value.Equals(abstractNumIdValue))
                            a2.SetValue(guidd);

                        guidd2++;
                    }
                }

                guidd++;
            }

            // Checking whether there were more than 0 elements, helped me get rid of exceptions thrown while using InsertDocument
            if (numbering.Root.Elements(XName.Get("abstractNum", w.NamespaceName)).Count() > 0)
                numbering.Root.Elements(XName.Get("abstractNum", w.NamespaceName)).Last().AddAfterSelf(remote_abstractNums);

            if (numbering.Root.Elements(XName.Get("num", w.NamespaceName)).Count() > 0)
                numbering.Root.Elements(XName.Get("num", w.NamespaceName)).Last().AddAfterSelf(remote_nums);
        }