Bentuk XML dokumen WordprocessingML (LINQ ke XML)

Artikel ini memperkenalkan bentuk XML dari dokumen WordprocessingML.

Format Microsoft Office

Format file asli untuk sistem Microsoft Office 2007 Office Open XML (umumnya disebut Open XML). Open XML adalah format berbasis XML yang merupakan standar Ecma dan saat ini sedang melalui proses standar ISO-IEC. Bahasa markup untuk file pemrosesan kata dalam Open XML disebut WordprocessingML. Tutorial ini menggunakan file sumber WordprocessingML sebagai input untuk contoh.

Jika Anda menggunakan Microsoft Office 2003, Anda bisa menyimpan dokumen dalam format Office Open XML jika Anda telah menginstal Paket Kompatibilitas Microsoft Office untuk Format File Word, Excel, dan PowerPoint 2007.

Bentuk dokumen WordprocessingML

Hal pertama yang perlu dipahami adalah bentuk XML dokumen WordprocessingML. Dokumen WordprocessingML berisi elemen isi (bernama w:body) yang berisi paragraf dokumen. Setiap paragraf berisi satu atau lebih eksekusi teks (bernama w:r). Setiap eksekusi teks berisi satu atau beberapa potongan teks (bernama w:t).

Berikut ini adalah dokumen WordprocessingML yang sangat sederhana:

<?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>

Dokumen ini berisi dua paragraf. Keduanya berisi satu eksekusi teks, dan setiap eksekusi teks berisi satu bagian teks.

Cara termudah untuk melihat konten dokumen WordprocessingML dalam formulir XML adalah dengan membuatnya menggunakan Microsoft Word, menyimpannya, lalu menjalankan program berikut yang mencetak XML ke konsol.

Contoh ini menggunakan kelas yang ditemukan di rakitan WindowsBase. Ini menggunakan jenis di namespace System.IO.Packaging.

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

Lihat juga