Beispiel für die Ausgabe von Office Open XML-Dokumentbausteinen (LINQ to XML)
In diesem Artikel wird gezeigt, wie Sie ein Office Open XML-Dokument öffnen und auf dessen Teile zugreifen.
Beispiel: Ausgeben des Dokument- und Formatvorlagenteils eines Office Open XML-Dokuments
Das folgende Beispiel öffnet ein Office Open XML-Dokument und gibt den Dokumentteil und den Formatvorlagenteil aus.
In diesem Beispiel werden Klassen aus der WindowsBase-Assembly und Typen aus dem System.IO.Packaging-Namespace verwendet.
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
Zusammenarbeit auf GitHub
Die Quelle für diesen Inhalt finden Sie auf GitHub, wo Sie auch Issues und Pull Requests erstellen und überprüfen können. Weitere Informationen finden Sie in unserem Leitfaden für Mitwirkende.
Feedback
https://aka.ms/ContentUserFeedback.
Bald verfügbar: Im Laufe des Jahres 2024 werden wir GitHub-Issues stufenweise als Feedbackmechanismus für Inhalte abbauen und durch ein neues Feedbacksystem ersetzen. Weitere Informationen finden Sie unterFeedback senden und anzeigen für