Blog - Title

Printing the Parts of an Open XML Document - VB

Printing the Parts of an Open XML Document - VB

  • Comments 0

[Table of Contents] [Next Topic]

An easy way to see the XML in the parts of a WordprocessingML document is to create one in your word processor, save it, and then run the following program that prints the XML to the console.  You can also rename the file, adding a ".zip" to the end, and then extract the parts.

This blog is inactive.
New blog: EricWhite.com/blog

Blog TOC
This example uses Open XML SDK 1.0.  The classes are in the DocumentFormat.OpenXml assemblies.  The namespace for the classes is DocumentFormat.OpenXml.Packaging.

Imports DocumentFormat.OpenXml.Packaging
Imports System.IO
Imports System.Xml
 
Module Module1
    Public Function LoadXDocument(ByVal part As OpenXmlPart) _
            As XDocument
        Using streamReader As StreamReader = New StreamReader(part.GetStream())
            Using xmlReader As XmlReader = xmlReader.Create(streamReader)
                Return XDocument.Load(xmlReader)
            End Using
        End Using
    End Function
 
    Sub Main()
        Dim filename As String = "Test.docx"
        Using wordDoc As WordprocessingDocument = _
            WordprocessingDocument.Open(filename, True)
            Dim mainPart As MainDocumentPart = _
                wordDoc.MainDocumentPart
            Dim xDoc As XDocument = LoadXDocument(mainPart)
            Console.WriteLine(xDoc)
        End Using
    End Sub
End Module
 

[Table of Contents] [Next Topic] [Blog Map]

Leave a Comment
  • Please add 5 and 6 and type the answer here:
  • Post