「WordprocessingML ドキュメント内の情報の操作」チュートリアルでの最初の作業は、ドキュメント内にある段落の既定のスタイルを検索することです。
例
説明
次の例では、Office Open XML WordprocessingML ドキュメントを開き、パッケージのドキュメント パーツとスタイル パーツを検索した後、既定のスタイル名を検索するクエリを実行します。 Office Open XML ドキュメント パッケージおよびその構成パーツの詳細については、「Office Open XML WordprocessingML ドキュメントの詳細」を参照してください。
このクエリは、値が "paragraph" である w:type という名前の属性と、値が "1" である w:default という名前の属性を持つ w:style という名前のノードを検索します。 これらの属性を持つ XML ノードは 1 つしかないため、このクエリは、Enumerable.First 演算子を使用してコレクションをシングルトンに変換します。 次に、w:styleId という名前の属性の値を取得します。
この例では、WindowsBase アセンブリのクラスを使用します。 また、System.IO.Packaging 名前空間内の型を使用します。
コード
const string fileName = "SampleDoc.docx";
const string documentRelationshipType =
"https://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument";
const string stylesRelationshipType =
"https://schemas.openxmlformats.org/officeDocument/2006/relationships/styles";
const string wordmlNamespace =
"https://schemas.openxmlformats.org/wordprocessingml/2006/main";
XNamespace w = wordmlNamespace;
XDocument xDoc = null;
XDocument styleDoc = null;
using (Package wdPackage = Package.Open(fileName, FileMode.Open, FileAccess.Read))
{
PackageRelationship docPackageRelationship =
wdPackage.GetRelationshipsByType(documentRelationshipType).FirstOrDefault();
if (docPackageRelationship != null)
{
Uri documentUri = PackUriHelper.ResolvePartUri(new Uri("/", UriKind.Relative),
docPackageRelationship.TargetUri);
PackagePart documentPart = wdPackage.GetPart(documentUri);
// Load the document XML in the part into an XDocument instance.
xDoc = XDocument.Load(XmlReader.Create(documentPart.GetStream()));
// Find the styles part. There will only be one.
PackageRelationship styleRelation =
documentPart.GetRelationshipsByType(stylesRelationshipType).FirstOrDefault();
if (styleRelation != null)
{
Uri styleUri = PackUriHelper.ResolvePartUri(documentUri, styleRelation.TargetUri);
PackagePart stylePart = wdPackage.GetPart(styleUri);
// Load the style XML in the part into an XDocument instance.
styleDoc = XDocument.Load(XmlReader.Create(stylePart.GetStream()));
}
}
}
// The following query finds all the paragraphs that have the default style.
string defaultStyle =
(string)(
from style in styleDoc.Root.Elements(w + "style")
where (string)style.Attribute(w + "type") == "paragraph"&&
(string)style.Attribute(w + "default") == "1"
select style
).First().Attribute(w + "styleId");
Console.WriteLine("The default style is: {0}", defaultStyle);
Imports <xmlns:w="https://schemas.openxmlformats.org/wordprocessingml/2006/main">
Module Module1
Sub Main()
Const fileName As String = "SampleDoc.docx"
Const documentRelationshipType As String = _
"https://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument"
Const stylesRelationshipType As String = _
"https://schemas.openxmlformats.org/officeDocument/2006/relationships/styles"
Dim xDoc As XDocument = Nothing
Dim styleDoc As XDocument = Nothing
Using wdPackage As Package = Package.Open(fileName, 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)
' Load the document XML in the part into an XDocument instance.
xDoc = XDocument.Load(XmlReader.Create(documentPart.GetStream()))
' Find the styles part. There will only be one.
Dim styleRelation As PackageRelationship = _
documentPart.GetRelationshipsByType(stylesRelationshipType).FirstOrDefault()
If styleRelation IsNot Nothing Then
Dim styleUri As Uri = _
PackUriHelper.ResolvePartUri(documentUri, styleRelation.TargetUri)
Dim stylePart As PackagePart = wdPackage.GetPart(styleUri)
' Load the style XML in the part into an XDocument instance.
styleDoc = XDocument.Load(XmlReader.Create(stylePart.GetStream()))
End If
End If
End Using
' The following query finds all the paragraphs that have the default style.
Dim defParas As IEnumerable(Of XElement) = _
From style In styleDoc.Root.<w:style> _
Where style.@w:type.Equals("paragraph") And _
style.@w:default.Equals("1") _
Select style
' Then find the style of the first.
Dim defaultStyle As String = defParas.First().@w:styleId
Console.WriteLine("The default style is: " & defaultStyle)
End Sub
End Module
コメント
この例では次の出力が生成されます。
The default style is: Normal
次の手順
次の例では、ドキュメント内のすべての段落およびそのスタイルを検索する同様のクエリを記述します。