OpenXmlPart.getXDocument()

Return to the
Open XML SDK for JavaScript
Developer Center
Retrieves the XML from this part as an XDocument object. This method has caching semantics, which means that you can call it multiple times without suffering a performance penalty.

Syntax

    part.getXDocument();

Return Value

    Returns an XDocument object that you can modify. When you serialize the package using the saveToBase64 or saveToFlatOpc functions, the modified XDocument will be serialized as well.

Usage

    var xDoc = pkg.mainDocumentPart().getXDocument();

Example

// Open a blank document that is stored as a base64 string.
var doc = new openXml.OpenXmlPackage(blankDocument_base64);
// Create a paragraph.
var p = new XElement(W.p,
    new XElement(W.r,
        new XElement(W.t, "Hello Open XML World")));
// Replace the first paragraph in the document with the new paragraph.
doc.mainDocumentPart()
    .getXDocument()
    .descendants(W.p)
    .firstOrDefault()
    .replaceWith(p);
return {
    returnedDocument: doc.saveToBase64(),
    defaultDocumentName: "OpenXmlDocument.docx",
    output: "Click the 'Save' button to save the modified document to a local drive.  If you do not see a 'Save' button, you do not have Flash available, and can't save the document."
};