WordprocessingML-dokumentumok XML-alakzata (LINQ–XML)

Ez a cikk egy WordprocessingML-dokumentum XML-alakját mutatja be.

Microsoft Office-formátumok

A 2007-ben használt Microsoft Office-rendszer natív fájlformátuma az Office Open XML (más néven Open XML). A nyílt XML egy Ecma-szabványnak számító XML-alapú formátum, amely jelenleg a ISO-IEC szabványfolyamaton megy keresztül. Az Open XML-fájl szövegszerkesztő fájljainak korrektúranyelvét WordprocessingML-nek nevezzük. Ez az oktatóanyag WordprocessingML-forrásfájlokat használ a példák bemeneteként.

Ha Microsoft Office 2003-at használ, a dokumentumokat Office Open XML formátumban mentheti, ha telepítette a Microsoft Office Word- és Excel-kompatibilitási csomagját, valamint a PowerPoint 2007-fájlformátumokat.

A WordprocessingML-dokumentumok alakja

Elsőként a WordprocessingML-dokumentumok XML-alakját kell megérteni. A WordprocessingML-dokumentum tartalmaz egy törzselemet (névvel w:body), amely tartalmazza a dokumentum bekezdéseit. Minden bekezdés egy vagy több (elnevezett w:r) szövegfuttatást tartalmaz. Minden egyes szövegfuttatás egy vagy több (nevesített w:t) szövegrészt tartalmaz.

A következő egy nagyon egyszerű WordprocessingML-dokumentum:

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

Ez a dokumentum két bekezdést tartalmaz. Mindkettő egyetlen szöveges futtatásból áll, és mindegyik szövegfuttatás egyetlen szöveges elemet tartalmaz.

Egy WordprocessingML-dokumentum tartalmát legegyszerűbben úgy tekintheti meg XML-formátumban, ha létrehoz egyet a Microsoft Word használatával, menti, majd futtatja a következő programot, amely kinyomtatja az XML-t a konzolon.

Ez a példa a WindowsBase-szerelvényben található osztályokat használja. Típusokat használ a System.IO.Packaging névtérben.

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

Lásd még