이 문서에서는 WordprocessingML 문서의 XML 셰이프를 소개합니다.
Microsoft Office 형식
2007 Microsoft Office 시스템의 네이티브 파일 형식은 Office Open XML(일반적으로 Open XML이라고 함)입니다. Open XML은 Ecma 표준이며 현재 ISO-IEC 표준 프로세스를 거치고 있는 XML 기반 형식입니다. Open XML 내의 단어 처리 파일에 대한 태그 언어를 WordprocessingML이라고 합니다. 이 자습서에서는 WordprocessingML 원본 파일을 예제의 입력으로 사용합니다.
Microsoft Office 2003을 사용하는 경우 Word, Excel 및 PowerPoint 2007 파일 형식용 Microsoft Office 호환성 팩을 설치한 경우 Office Open XML 형식으로 문서를 저장할 수 있습니다.
WordprocessingML 문서의 모양
가장 먼저 이해해야 할 것은 WordprocessingML 문서의 XML 셰이프입니다. WordprocessingML 문서에는 문서의 단락이 포함된 본문 요소(이름) w:body
가 포함됩니다. 각 단락에는 하나 이상의 텍스트 런(명명된 w:r
)이 포함됩니다. 각 텍스트 실행에는 하나 이상의 텍스트 조각(이름) 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>
이 문서에는 두 개의 단락이 포함되어 있습니다. 둘 다 단일 텍스트 실행을 포함하고 각 텍스트 실행에는 단일 텍스트 조각이 포함됩니다.
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
참고하십시오
.NET