Freigeben über


Comparison of Navigating Parts between System.IO.Packaging and the Open XML SDK

[Blog Map]

Code highlighted in yellow shows navigating from the package to the main document part. Code highlighted in green shows navigating from the main document part to the styles part.

using System;

using System.Linq;

using System.IO;

using System.IO.Packaging;

using System.Xml;

using System.Xml.Linq;

using DocumentFormat.OpenXml.Packaging;

class Program

{

    static void SystemIoPackaging()

    {

        const string fileName = "Test.docx";

        const string documentRelationshipType =

          "https://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument";

        const string stylesRelationshipType =

          "https://schemas.openxmlformats.org/officeDocument/2006/relationships/styles";

        XDocument xDocMainDocument = null;

        XDocument xDocStyleDocument = null;

        using (Package wdPackage = Package.Open(fileName, FileMode.Open, FileAccess.Read))

        {

            PackageRelationship docPackageRelationship = wdPackage

                .GetRelationshipsByType(documentRelationshipType)

                .FirstOrDefault();

            if (docPackageRelationship != null)

            {

                Uri documentUri = PackUriHelper.ResolvePartUri(

                       new Uri("/", UriKind.Relative), docPackageRelationship.TargetUri);

                PackagePart documentPart = wdPackage.GetPart(documentUri);

                // Load the document XML in the part into an XDocument instance.

                using (Stream documentPartStream = documentPart.GetStream())

                using (XmlReader documentPartXmlReader =

                       XmlReader.Create(documentPart.GetStream()))

                    xDocMainDocument = XDocument.Load(documentPartXmlReader);

                // Find the styles part. There will only be one.

                PackageRelationship styleRelation = documentPart

                    .GetRelationshipsByType(stylesRelationshipType)

                    .FirstOrDefault();

                if (styleRelation != null)

                {

                    Uri styleUri = PackUriHelper.ResolvePartUri(documentUri,

                        styleRelation.TargetUri);

       PackagePart stylePart = wdPackage.GetPart(styleUri);

                    // Load the style XML in the part into an XDocument instance.

                    using (Stream stylePartStream = stylePart.GetStream())

                    using (XmlReader stylePartXmlReader =

                           XmlReader.Create(stylePartStream))

                        xDocStyleDocument = XDocument.Load(stylePartXmlReader);

                }

            }

        }

        Console.WriteLine("The main document part has {0} nodes.",

            xDocMainDocument.DescendantNodes().Count());

        Console.WriteLine("The style part has {0} nodes.",

            xDocStyleDocument.DescendantNodes().Count());

    }

    static void OpenXmlSdk()

    {

        const string filename = "Test.docx";

        using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(filename, true))

        {

            MainDocumentPart mainPart = wordDoc.MainDocumentPart;

            StyleDefinitionsPart styleDefinitionsPart = mainPart.StyleDefinitionsPart;

            Console.WriteLine("The main document part has {0} nodes.",

                mainPart.RootElement.Descendants().Count());

            Console.WriteLine("The style part has {0} nodes.",

      styleDefinitionsPart.RootElement.Descendants().Count());

        }

    }

    static void Main(string[] args)

    {

        SystemIoPackaging();

        OpenXmlSdk();

    }

}