Freigeben über


Comparison of LINQ to XM and the Open XML SDK Strongly Typed OM

LINQ to XML code is highlighted in yellow.  Open XML SDK strongly typed object model is highlighted in green.

This blog is inactive.
New blog: EricWhite.com/blog

Blog TOC

using System;
using System.IO;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;

public static class MyExtensions
{
public static XDocument GetXDocument(this OpenXmlPart part)
{
XDocument xdoc = part.Annotation<XDocument>();
if (xdoc != null)
return xdoc;
using (StreamReader streamReader = new StreamReader(part.GetStream()))
xdoc = XDocument.Load(XmlReader.Create(streamReader));
part.AddAnnotation(xdoc);
return xdoc;
}
}

class Program
{
static void LinqToXml()
{
const string filename = "Test.docx";

using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(filename, true))
{
XNamespace w =
"https://schemas.openxmlformats.org/wordprocessingml/2006/main";

MainDocumentPart mainPart = wordDoc.MainDocumentPart;
StyleDefinitionsPart styleDefinitionsPart = mainPart.StyleDefinitionsPart;
XDocument mainPartXDocument = mainPart.GetXDocument();
int paragraphs = mainPartXDocument.Descendants(w + "p").Count();
int distinctStyles = mainPartXDocument
.Descendants(w + "p")
.Select(p => (string)p
.Elements(w + "pPr")
.Elements(w + "pStyle")
.Attributes(w + "val")
.FirstOrDefault())
.Distinct()
.Count();

Console.WriteLine("The main document part has {0} paragraphs.",
paragraphs);
Console.WriteLine("There are {0} distinct styles used", distinctStyles);
}
}

static void OpenXmlSdk()
{
const string filename = "Test.docx";

using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(filename, true))
{
MainDocumentPart mainPart = wordDoc.MainDocumentPart;
StyleDefinitionsPart styleDefinitionsPart = mainPart.StyleDefinitionsPart;

int paragraphs = mainPart.RootElement.Descendants<Paragraph>().Count();

int distinctStyles = mainPart
.Document
.Descendants<Paragraph>()
.Select(p =>
{
string styleName = "";
var pp = p.GetFirstChild<ParagraphProperties>();
if (pp != null)
{
if (pp.ParagraphStyleId != null)
styleName = pp.ParagraphStyleId.Val;
}
return styleName;
})
.Distinct()
.Count();

Console.WriteLine("The main document part has {0} paragraphs.",
paragraphs);
Console.WriteLine("There are {0} distinct styles used", distinctStyles);
}
}
    static void Main(string[] args)
{
LinqToXml();
OpenXmlSdk();
}
}