private void merge_endnotes(PackagePart remote_pp, PackagePart local_pp, XDocument remote_mainDoc, DocX remote, XDocument remote_endnotes)
{
IEnumerable<int> ids =
(
from d in endnotes.Root.Descendants()
where d.Name.LocalName == "endnote"
select int.Parse(d.Attribute(XName.Get("id", w.NamespaceName)).Value)
);
int max_id = ids.Max() + 1;
var endnoteReferences = remote_mainDoc.Descendants(XName.Get("endnoteReference", w.NamespaceName));
foreach (var endnote in remote_endnotes.Root.Elements().OrderBy(fr => fr.Attribute(XName.Get("id", r.NamespaceName))).Reverse())
{
XAttribute id = endnote.Attribute(XName.Get("id", w.NamespaceName));
int i;
if (id != null && int.TryParse(id.Value, out i))
{
if (i > 0)
{
foreach (var endnoteRef in endnoteReferences)
{
XAttribute a = endnoteRef.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.
endnote.SetAttributeValue(XName.Get("id", w.NamespaceName), max_id);
endnotes.Root.Add(endnote);
max_id++;
}
}
}
}