using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Xml; using System.Xml.Linq; using DocumentFormat.OpenXml.Packaging; class Program { public static XDocument LoadXDocument(OpenXmlPart part) { XDocument xdoc; using (StreamReader streamReader = new StreamReader(part.GetStream())) xdoc = XDocument.Load(XmlReader.Create(streamReader)); return xdoc; } static void Main(string[] args) { const string filename = "SampleDoc.docx"; using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(filename, true)) { MainDocumentPart mainPart = wordDoc.MainDocumentPart; StyleDefinitionsPart styleDefinitionsPart = mainPart.StyleDefinitionsPart; CommentsPart commentsPart = mainPart.CommentsPart; XDocument xDoc = LoadXDocument(mainPart); XDocument styleDoc = LoadXDocument(styleDefinitionsPart); XDocument commentsDoc = LoadXDocument(commentsPart); Console.WriteLine("The main document part has {0} nodes.", xDoc.DescendantNodes().Count()); Console.WriteLine("The style part has {0} nodes.", styleDoc.DescendantNodes().Count()); Console.WriteLine("The comments part has {0} nodes.", commentsDoc.DescendantNodes().Count()); } } }