Notitie
Voor toegang tot deze pagina is autorisatie vereist. U kunt proberen u aan te melden of de directory te wijzigen.
Voor toegang tot deze pagina is autorisatie vereist. U kunt proberen de mappen te wijzigen.
In dit artikel wordt de XML-vorm van een WordprocessingML-document geïntroduceerd.
Microsoft Office-indelingen
De systeemeigen bestandsindeling voor het Microsoft Office-systeem 2007 is Office Open XML (ook wel Open XML genoemd). Open XML is een XML-gebaseerd formaat dat een Ecma-standaard is en momenteel door het ISO-IEC-standaardproces gaat. De opmaaktaal voor tekstverwerkingsbestanden in Open XML wordt WordprocessingML genoemd. In deze zelfstudie worden wordprocessingML-bronbestanden gebruikt als invoer voor de voorbeelden.
Als u Microsoft Office 2003 gebruikt, kunt u documenten opslaan in de Office Open XML-indeling als u het Microsoft Office-compatibiliteitspakket voor Word, Excel en PowerPoint 2007-bestandsindelingen hebt geïnstalleerd.
De vorm van WordprocessingML-documenten
Het eerste wat u moet begrijpen, is de XML-vorm van WordprocessingML-documenten. Een WordprocessingML-document bevat een hoofdtekstelement (benoemd w:body) dat de alinea's van het document bevat. Elke alinea bevat een of meer tekstuitvoeringen (benoemd w:r). Elke tekstuitvoering bevat een of meer tekststukken (benoemd w:t).
Hier volgt een heel eenvoudig WordprocessingML-document:
<?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>
Dit document bevat twee alinea's. Ze bevatten beide één tekstuitvoering en elke tekstuitvoering bevat één tekststuk.
De eenvoudigste manier om de inhoud van een WordprocessingML-document in XML-formulier te bekijken, is door er een te maken met Microsoft Word, op te slaan en vervolgens het volgende programma uit te voeren waarmee de XML naar de console wordt afgedrukt.
In dit voorbeeld worden klassen gebruikt die zijn gevonden in de WindowsBase-assembly. Er worden typen in de System.IO.Packaging naamruimte gebruikt.
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