Hinweis
Für den Zugriff auf diese Seite ist eine Autorisierung erforderlich. Sie können versuchen, sich anzumelden oder das Verzeichnis zu wechseln.
Für den Zugriff auf diese Seite ist eine Autorisierung erforderlich. Sie können versuchen, das Verzeichnis zu wechseln.
In diesem Artikel wird die XML-Form eines WordprocessingML-Dokuments vorgestellt.
Microsoft Office-Formate
Das systemeigene Dateiformat für das Microsoft Office System 2007 ist Office Open XML (allgemein Als Open XML bezeichnet). Open XML ist ein XML-basiertes Format, das ein Ecma-Standard ist und derzeit den ISO-IEC Standardsprozess durchläuft. Die Markupsprache für Textverarbeitungsdateien in Open XML wird als WordprocessingML bezeichnet. In diesem Lernprogramm werden WordprocessingML-Quelldateien als Eingabe für die Beispiele verwendet.
Wenn Sie Microsoft Office 2003 verwenden, können Sie Dokumente im Office Open XML-Format speichern, wenn Sie das Microsoft Office Compatibility Pack für Word, Excel und PowerPoint 2007-Dateiformate installiert haben.
Die Form von WordprocessingML-Dokumenten
Das Erste, was zu verstehen ist, besteht in der XML-Form von WordprocessingML-Dokumenten. Ein WordprocessingML-Dokument enthält ein Textkörperelement (benannt w:body), das die Absätze des Dokuments enthält. Jeder Absatz enthält einen oder mehrere Textläufe (benannt w:r). Jeder Textlauf enthält mindestens einen Textabschnitt (benannt w:t).
Es folgt ein sehr einfaches WordprocessingML-Dokument:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<w:document
xmlns:ve="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"
xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math"
xmlns:v="urn:schemas-microsoft-com:vml"
xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"
xmlns:w10="urn:schemas-microsoft-com:office:word"
xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml">
<w:body>
<w:p w:rsidR="00E22EB6"
w:rsidRDefault="00E22EB6">
<w:r>
<w:t>This is a paragraph.</w:t>
</w:r>
</w:p>
<w:p w:rsidR="00E22EB6"
w:rsidRDefault="00E22EB6">
<w:r>
<w:t>This is another paragraph.</w:t>
</w:r>
</w:p>
</w:body>
</w:document>
Dieses Dokument enthält zwei Absätze. Beide enthalten einen einzelnen Textlauf, und jeder Textlauf enthält einen einzelnen Textabschnitt.
Am einfachsten können Sie den Inhalt eines WordprocessingML-Dokuments in XML-Formularen anzeigen, indem Sie ein Dokument mit Microsoft Word erstellen, speichern und dann das folgende Programm ausführen, das den XML-Code in der Konsole druckt.
In diesem Beispiel werden Klassen verwendet, die in der WindowsBase-Assembly zu finden sind. Es verwendet Typen im System.IO.Packaging Namespace.
const string documentRelationshipType =
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument";
const string wordmlNamespace =
"http://schemas.openxmlformats.org/wordprocessingml/2006/main";
XNamespace w = wordmlNamespace;
using (Package wdPackage = Package.Open("SampleDoc.docx", FileMode.Open, FileAccess.Read))
{
PackageRelationship relationship =
wdPackage
.GetRelationshipsByType(documentRelationshipType)
.FirstOrDefault();
if (relationship != null)
{
Uri documentUri =
PackUriHelper.ResolvePartUri(
new Uri("/", UriKind.Relative),
relationship.TargetUri);
PackagePart documentPart = wdPackage.GetPart(documentUri);
// Get the officeDocument part from the package.
// Load the XML in the part into an XDocument instance.
XDocument xdoc =
XDocument.Load(XmlReader.Create(documentPart.GetStream()));
Console.WriteLine(xdoc.Root);
}
}
Imports <xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
Module Module1
Sub Main()
Dim documentRelationshipType = _
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument"
Using wdPackage As Package = _
Package.Open("SampleDoc.docx", _
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)
' Get the officeDocument part from the package.
' Load the XML in the part into an XDocument instance.
Dim xDoc As XDocument = _
XDocument.Load(XmlReader.Create(documentPart.GetStream()))
Console.WriteLine(xDoc.Root)
End If
End Using
End Sub
End Module