Dela via


XML-formen för WordprocessingML-dokument (LINQ till XML)

Den här artikeln introducerar XML-formen för ett WordprocessingML-dokument.

Microsoft Office-format

Det interna filformatet för Microsoft Office-systemet 2007 är Office Open XML (kallas ofta Öppna XML). Open XML är ett XML-baserat format som är en Ecma-standard och som för närvarande genomgår ISO-IEC-standardprocessen. Påläggsspråket för ordbearbetningsfiler i Open XML kallas WordprocessingML. I den här självstudien används WordprocessingML-källfiler som indata för exemplen.

Om du använder Microsoft Office 2003 kan du spara dokument i Office Open XML-format om du har installerat Microsoft Office Compatibility Pack för Word-, Excel- och PowerPoint 2007-filformat.

Formen på WordprocessingML-dokument

Det första du behöver förstå är XML-formen för WordprocessingML-dokument. Ett WordprocessingML-dokument innehåller ett brödtextelement (med namnet w:body) som innehåller styckena i dokumentet. Varje stycke innehåller en eller flera textkörningar (med namnet w:r). Varje textkörning innehåller en eller flera textstycken (med namnet w:t).

Följande är ett mycket enkelt 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>

Det här dokumentet innehåller två stycken. Båda innehåller en enda textkörning och varje textkörning innehåller ett enda textstycke.

Det enklaste sättet att se innehållet i ett WordprocessingML-dokument i XML-formulär är att skapa ett med Hjälp av Microsoft Word, spara det och sedan köra följande program som skriver ut XML till konsolen.

I det här exemplet används klasser som finns i WindowsBase-sammansättningen. Den använder typer i System.IO.Packaging namnområdet.

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

Se även