次の方法で共有


WordprocessingML ドキュメントの XML 図形 (LINQ to XML)

この記事では、WordprocessingML ドキュメントの XML 図形について説明します。

Microsoft Office の形式

2007 Microsoft Office システムのネイティブ ファイル形式は、Office Open XML (一般に Open XML と呼ばれます) です。 Open XML は、Ecma 標準である XML ベースの形式であり、現在、ISO-IEC 標準プロセスを実行しています。 Open XML 内のワープロ ファイルのマークアップ言語は、WordprocessingML と呼ばれます。 このチュートリアルでは、例の入力として WordprocessingML ソース ファイルを使用します。

Microsoft Office 2003 を使用している場合は、Word、Excel、および PowerPoint 2007 ファイル形式の Microsoft Office 互換機能パックをインストールしている場合は、Office Open XML 形式でドキュメントを保存できます。

WordprocessingML ドキュメントの形状

最初に理解するのは、WordprocessingML ドキュメントの XML 形式です。 WordprocessingML ドキュメントには、文書の段落を含む本文要素 ( w:body) が含まれています。 各段落には、1 つ以上のテキストラン ( w:r という名前) が含まれています。 各テキスト ランには、1 つ以上のテキスト部分 ( w:t という名前) が含まれています。

非常に単純な WordprocessingML ドキュメントを次に示します。

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

このドキュメントには、2 つの段落が含まれています。 どちらも 1 つのテキスト ランを含み、各テキスト ランには 1 つのテキスト部分が含まれています。

WordprocessingML ドキュメントの内容を XML 形式で表示する最も簡単な方法は、Microsoft Word を使用して文書を作成し、保存してから、コンソールに XML を出力する次のプログラムを実行することです。

この例では、WindowsBase アセンブリにあるクラスを使用します。 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

こちらも参照ください