Example that outputs Office Open XML document parts (LINQ to XML)
This article shows how to open an Office Open XML document and access its parts.
Example: Print the document and style parts of an Office Open XML document
The following example opens an Office Open XML document and prints the document and styles parts.
This example uses classes from the WindowsBase assembly, and types from the System.IO.Packaging namespace.
const string fileName = "SampleDoc.docx";
const string documentRelationshipType =
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument";
const string stylesRelationshipType =
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles";
const string wordmlNamespace =
"http://schemas.openxmlformats.org/wordprocessingml/2006/main";
XNamespace w = wordmlNamespace;
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.
XDocument xdoc = XDocument.Load(XmlReader.Create(documentPart.GetStream()));
Console.WriteLine("TargetUri:{0}", docPackageRelationship.TargetUri);
Console.WriteLine("==================================================================");
Console.WriteLine(xdoc.Root);
Console.WriteLine();
// 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.
XDocument styleDoc = XDocument.Load(XmlReader.Create(stylePart.GetStream()));
Console.WriteLine("TargetUri:{0}", styleRelation.TargetUri);
Console.WriteLine("==================================================================");
Console.WriteLine(styleDoc.Root);
Console.WriteLine();
}
}
}
Const fileName As String = "SampleDoc.docx"
Const documentRelationshipType As String = _
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument"
Const stylesRelationshipType As String = _
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles"
Const wordmlNamespace As String = _
"http://schemas.openxmlformats.org/wordprocessingml/2006/main"
Dim w As XNamespace = wordmlNamespace
Using wdPackage As Package = Package.Open(fileName, FileMode.Open, FileAccess.Read)
Dim docPackageRelationship As PackageRelationship = _
wdPackage.GetRelationshipsByType(documentRelationshipType).FirstOrDefault()
If docPackageRelationship IsNot Nothing Then
Dim documentUri As Uri = PackUriHelper.ResolvePartUri(New Uri("/", UriKind.Relative), _
docPackageRelationship.TargetUri)
Dim documentPart As PackagePart = wdPackage.GetPart(documentUri)
' Load the document XML in the part into an XDocument instance.
Dim xdoc As XDocument = XDocument.Load(XmlReader.Create(documentPart.GetStream()))
Console.WriteLine("TargetUri:{0}", docPackageRelationship.TargetUri)
Console.WriteLine("==================================================================")
Console.WriteLine(xdoc.Root)
Console.WriteLine()
' Find the styles part. There will only be one.
Dim styleRelation As PackageRelationship = _
documentPart.GetRelationshipsByType(stylesRelationshipType).FirstOrDefault()
If styleRelation IsNot Nothing Then
Dim styleUri As Uri = _
PackUriHelper.ResolvePartUri(documentUri, styleRelation.TargetUri)
Dim stylePart As PackagePart = wdPackage.GetPart(styleUri)
' Load the style XML in the part into an XDocument instance.
Dim styleDoc As XDocument = XDocument.Load(XmlReader.Create(stylePart.GetStream()))
Console.WriteLine("TargetUri:{0}", styleRelation.TargetUri)
Console.WriteLine("==================================================================")
Console.WriteLine(styleDoc.Root)
Console.WriteLine()
End If
End If
End Using
Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, see our contributor guide.
Feedback
https://aka.ms/ContentUserFeedback.
Coming soon: Throughout 2024 we will be phasing out GitHub Issues as the feedback mechanism for content and replacing it with a new feedback system. For more information see:Submit and view feedback for