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; public static class LocalExtensions { public static string GetPath(this XElement el) { return el .AncestorsAndSelf() .Aggregate("", (seed, i) => i.Name.LocalName + "/" + seed); } public static string StringConcatenate( this IEnumerable source) { return source.Aggregate( new StringBuilder(), (s, i) => s.Append(i), s => s.ToString()); } public static string StringConcatenate( this IEnumerable source, Func projectionFunc) { return source.Aggregate( new StringBuilder(), (s, i) => s.Append(projectionFunc(i)), s => s.ToString()); } } class Program { readonly static XNamespace w = "http://schemas.openxmlformats.org/wordprocessingml/2006/main"; public static XDocument LoadXDocument(OpenXmlPart part) { XDocument xdoc; using (StreamReader streamReader = new StreamReader(part.GetStream())) xdoc = XDocument.Load(XmlReader.Create(streamReader)); return xdoc; } public static string GetParagraphStyle(XElement para) { return (string)para.Elements(w + "pPr") .Elements(w + "pStyle") .Attributes(w + "val") .FirstOrDefault(); } static void Main(string[] args) { const string filename = "SampleDoc.docx"; using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(filename, true)) { MainDocumentPart mainPart = wordDoc.MainDocumentPart; StyleDefinitionsPart stylePart = mainPart.StyleDefinitionsPart; XDocument mainPartDoc = LoadXDocument(mainPart); XDocument styleDoc = LoadXDocument(stylePart); string defaultStyle = (string)styleDoc.Root .Elements(w + "style") .Where(style => (string)style.Attribute(w + "type") == "paragraph" && (string)style.Attribute(w + "default") == "1") .First() .Attribute(w + "styleId"); var paragraphs = mainPartDoc.Root .Element(w + "body") .Descendants(w + "p") .Select(p => { string style = GetParagraphStyle(p); string styleName = style == null ? defaultStyle : style; return new { ParagraphNode = p, Style = styleName }; } ); XName r = w + "r"; XName ins = w + "ins"; var paragraphsWithText = paragraphs.Select(p => new { ParagraphNode = p.ParagraphNode, Style = p.Style, Text = p.ParagraphNode .Elements() .Where(z => z.Name == r || z.Name == ins) .Descendants(w + "t") .StringConcatenate(s => (string)s) } ); foreach (var p in paragraphsWithText) Console.WriteLine("{0} {1}", p.Style != null ? p.Style.PadRight(12) : "".PadRight(12), p.Text); } } }