class Program
{
static void Main(string[] args)
{
string docName = "OpenXML.docx";
string authorName = "Gary zhang";
AcceptRevisions(docName, authorName);
}
public static void AcceptRevisions(string fileName, string authorName)
{
// Given a document name and an author name, accept revisions.
using (WordprocessingDocument wdDoc = WordprocessingDocument.Open(fileName, true))
{
Body body = wdDoc.MainDocumentPart.Document.Body;
// Handle the formatting changes.
List changes =
body.Descendants()
.Where(c => c.Author.Value == authorName).Cast().ToList();
foreach (OpenXmlElement change in changes)
{
change.Remove();
}
// Handle the deletions.
List deletions =
body.Descendants()
.Where(c => c.Author.Value == authorName).Cast().ToList();
deletions.AddRange(body.Descendants()
.Where(c => c.Author.Value == authorName).Cast().ToList());
deletions.AddRange(body.Descendants()
.Where(c => c.Author.Value == authorName).Cast().ToList());
foreach (OpenXmlElement deletion in deletions)
{
deletion.Remove();
}
// Handle the insertions.
List insertions =
body.Descendants()
.Where(c => c.Author.Value == authorName).Cast().ToList();
insertions.AddRange(body.Descendants()
.Where(c => c.Author.Value == authorName).Cast().ToList());
insertions.AddRange(body.Descendants()
.Where(c => c.Author.Value == authorName).Cast().ToList());
foreach (OpenXmlElement insertion in insertions)
{
// Found new content.
// Promote them to the same level as node, and then delete the node.
foreach (var run in insertion.Elements())
{
if (run == insertion.FirstChild)
{
insertion.InsertAfterSelf(new Run(run.OuterXml));
}
else
{
insertion.NextSibling().InsertAfterSelf(new Run(run.OuterXml));
}
}
insertion.RemoveAttribute("rsidR",
"http://schemas.openxmlformats.org/wordprocessingml/2006/main");
insertion.RemoveAttribute("rsidRPr",
"http://schemas.openxmlformats.org/wordprocessingml/2006/main");
insertion.Remove();
}
}
}
}