Created
October 30, 2014 14:11
-
-
Save benhysell/952f87a05dee783270a9 to your computer and use it in GitHub Desktop.
Merge Numbering DocX
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", DocX.w.NamespaceName)); | |
//get next abstract number from base document | |
var documentAbstractNumbers = numbering.Root.Elements(XName.Get("abstractNum", DocX.w.NamespaceName)); | |
int guidd = 0; | |
foreach (var an in documentAbstractNumbers) | |
{ | |
XAttribute a = an.Attribute(XName.Get("abstractNumId", DocX.w.NamespaceName)); | |
if (a != null) | |
{ | |
int i; | |
if (int.TryParse(a.Value, out i)) | |
{ | |
if (i > guidd) | |
guidd = i; | |
} | |
} | |
} | |
//guidd++; | |
//now get the abstractNumId from our new document | |
foreach (var an in remote_abstractNums) | |
{ | |
XAttribute a = an.Attribute(XName.Get("abstractNumId", DocX.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", DocX.w.NamespaceName)); | |
//get remote num from document | |
//get next abstract number from base document | |
var documentRemoteNumbers = numbering.Root.Elements(XName.Get("num", DocX.w.NamespaceName)); | |
int guidd2 = 0; | |
foreach (var an in documentRemoteNumbers) | |
{ | |
XAttribute a = an.Attribute(XName.Get("numId", DocX.w.NamespaceName)); | |
if (a != null) | |
{ | |
int i; | |
if (int.TryParse(a.Value, out i)) | |
{ | |
if (i > guidd2) | |
guidd2 = i; | |
} | |
} | |
} | |
//guidd2++; | |
foreach (var an in remote_nums) | |
{ | |
XAttribute a = an.Attribute(XName.Get("numId", DocX.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", DocX.w.NamespaceName)); | |
if (abstractNumId != null) | |
{ | |
String abstractNumIdValue = abstractNumId.Value; | |
abstractNumId.SetValue(guidd); | |
foreach (var nsid in remote_abstractNum.Descendants(XName.Get("nsid", DocX.w.NamespaceName))) | |
{ | |
//update the nsid to match the abstract number...in the odd event a document is merged in and they | |
//have the same abstract nsid. This will ensure counting will be restarted | |
//future, continue continuing on merge | |
var attr = nsid.Attribute(XName.Get("val", DocX.w.NamespaceName)); | |
if(null != attr) | |
attr.SetValue(guidd); | |
} | |
foreach (XElement remote_num in remote_nums) | |
{ | |
var numIds = remote_mainDoc.Descendants(XName.Get("numId", DocX.w.NamespaceName)); | |
foreach (var numId in numIds) | |
{ | |
XAttribute attr = numId.Attribute(XName.Get("val", DocX.w.NamespaceName)); | |
if (attr != null && attr.Value.Equals(remote_num.Attribute(XName.Get("numId", DocX.w.NamespaceName)).Value)) | |
{ | |
attr.SetValue(guidd2); | |
} | |
} | |
remote_num.SetAttributeValue(XName.Get("numId", DocX.w.NamespaceName), guidd2); | |
XElement e = remote_num.Element(XName.Get("abstractNumId", DocX.w.NamespaceName)); | |
if (e != null) | |
{ | |
XAttribute a2 = e.Attribute(XName.Get("val", DocX.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", DocX.w.NamespaceName)).Count() > 0) | |
numbering.Root.Elements(XName.Get("abstractNum", DocX.w.NamespaceName)).Last().AddAfterSelf(remote_abstractNums); | |
else | |
{ | |
//there is an odd case where the base document thinks it has number in it, when it doesn't | |
numbering.Root.Add(remote_abstractNums); | |
} | |
if (numbering.Root.Elements(XName.Get("num", DocX.w.NamespaceName)).Count() > 0) | |
numbering.Root.Elements(XName.Get("num", DocX.w.NamespaceName)).Last().AddAfterSelf(remote_nums); | |
else | |
{ | |
numbering.Root.Add(remote_nums); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks for posting this solution however i am getting the following error "The name 'numbering' does not exist in the current context" any help on how to fix that? thanks