Novacode.DocX.merge_footnotes C# (CSharp) Method

merge_footnotes() private method

private merge_footnotes ( PackagePart remote_pp, PackagePart local_pp, System.Xml.Linq.XDocument remote_mainDoc, DocX remote, System.Xml.Linq.XDocument remote_footnotes ) : void
remote_pp System.IO.Packaging.PackagePart
local_pp System.IO.Packaging.PackagePart
remote_mainDoc System.Xml.Linq.XDocument
remote DocX
remote_footnotes System.Xml.Linq.XDocument
return void
        private void merge_footnotes(PackagePart remote_pp, PackagePart local_pp, XDocument remote_mainDoc, DocX remote, XDocument remote_footnotes)
        {
            IEnumerable<int> ids =
            (
                from d in footnotes.Root.Descendants()
                where d.Name.LocalName == "footnote"
                select int.Parse(d.Attribute(XName.Get("id", w.NamespaceName)).Value)
            );

            int max_id = ids.Max() + 1;
            var footnoteReferences = remote_mainDoc.Descendants(XName.Get("footnoteReference", w.NamespaceName));

            foreach (var footnote in remote_footnotes.Root.Elements().OrderBy(fr => fr.Attribute(XName.Get("id", r.NamespaceName))).Reverse())
            {
                XAttribute id = footnote.Attribute(XName.Get("id", w.NamespaceName));
                int i;
                if (id != null && int.TryParse(id.Value, out i))
                {
                    if (i > 0)
                    {
                        foreach (var footnoteRef in footnoteReferences)
                        {
                            XAttribute a = footnoteRef.Attribute(XName.Get("id", w.NamespaceName));
                            if (a != null && int.Parse(a.Value).Equals(i))
                            {
                                a.SetValue(max_id);
                            }
                        }

                        // We care about copying this footnote.
                        footnote.SetAttributeValue(XName.Get("id", w.NamespaceName), max_id);
                        footnotes.Root.Add(footnote);
                        max_id++;
                    }
                }
            }
        }