Search code examples
ms-wordlinq-to-xmlopenxmlopenxml-sdk

How do I save Xml Changes Back to the Original Document


I need to update the Styles (styles.xml) part of an MS Word document due to a problem with a vendor's product.

So far I've been able to extract and update the xml I need. The only problem, is that I don't know how to save my changes back to the document.

The code below is working just fine. I usually output the xml to the console to make sure it's going in just fine. At the end, I know I need to perform some save operation, but the XDocument.Save( /stream/) hasn't worked.

Here's where I am so far

static void FixNormal()
{   

    using (WordprocessingDocument doc = WordprocessingDocument.Open(_path, true))
    {
        // Get the Styles part for this document.
        StyleDefinitionsPart stylesPart = doc.MainDocumentPart.StyleDefinitionsPart;


        // If the Styles part does not exist, add it and then add the style.
        if (stylesPart == null)
        {
            Console.WriteLine("No Style Part");

        }
        else
        {
            XDocument stylesDoc;

            using (var reader = XmlNodeReader.Create(stylesPart.GetStream(FileMode.Open, FileAccess.Read)))
            {


                XNamespace w = "http://schemas.openxmlformats.org/wordprocessingml/2006/main";
                Console.WriteLine(stylesPart.Styles.OuterXml);
                // Create the XDocument.
                stylesDoc = XDocument.Load(reader);

                var xStyle = stylesDoc.Descendants(w + "styles").Descendants(w + "style").Where(x => x.Attribute(w + "styleId").Value.Equals("Normal"));
                XElement style = xStyle.Single();



                var q = style.Descendants(w + "qFormat").FirstOrDefault();
                if (q is null)
                {
                    XElement qFormat = new XElement(w + "qFormat");
                    style.Add(qFormat);


                }

                var r = style.Descendants(w + "rsid").FirstOrDefault();
                if (r is null)
                {
                    XElement rsid = new XElement(w + "rsid");
                    XAttribute val = new XAttribute(w + "val", "003C4F1E");
                    rsid.Add(val);

                    style.Add(rsid);                                                      

                }

            }

            //doc.Save(); --- Did not work 

        }

    }

}



Solution

  • I found the answer in the SAVE THE PARTS section of this page Replace the styles parts in a word processing document (Open XML SDK)

    See the end of this code for the solution. You'll also see what I've tried.

    static void FixNormal()
    {   
    
        using (WordprocessingDocument doc = WordprocessingDocument.Open(_path, true))
        {
            // Get the Styles part for this document.
            StyleDefinitionsPart stylesPart = doc.MainDocumentPart.StyleDefinitionsPart;
    
            // If the Styles part does not exist, add it and then add the style.
            if (stylesPart == null)
            {
                Console.WriteLine("No Style Part");
    
            }
            else
            {
                XDocument stylesDoc;
    
                using (var reader = XmlNodeReader.Create(stylesPart.GetStream(FileMode.Open, FileAccess.Read)))
                {
    
    
                    XNamespace w = "http://schemas.openxmlformats.org/wordprocessingml/2006/main";
    
                    // Create the XDocument.
                    stylesDoc = XDocument.Load(reader);
    
    
                    var xStyle = stylesDoc.Descendants(w + "styles").Descendants(w + "style").Where(x => x.Attribute(w + "styleId").Value.Equals("Normal"));
                    XElement style = xStyle.Single();
    
    
    
                    var q = style.Descendants(w + "qFormat").FirstOrDefault();
                    if (q is null)
                    {
                        XElement qFormat = new XElement(w + "qFormat");
                        style.Add(qFormat);
    
    
                    }
    
                    var r = style.Descendants(w + "rsid").FirstOrDefault();
                    if (r is null)
                    {
                        XElement rsid = new XElement(w + "rsid");
                        XAttribute val = new XAttribute(w + "val", "003C4F1E");
                        rsid.Add(val);
    
                        style.Add(rsid);                                                      
    
                    }
    
                }
    
                //doc.Save(); --- Did not work 
    
                //stylesDoc.Save(@"C:\WinTest\HooRah.xml"); -- I only use this to verify that I've updated everything correctly
    
                //using (XmlWriter xw = XmlWriter.Create(stylesPart.GetStream(FileMode.Create, FileAccess.Write)))
                //{
                //    stylesDoc.Save(xw);  -- DID NOT WORK EITHER
                //    doc.Save();
                //}
    
                // THIS WORKED
                stylesDoc.Save(new StreamWriter(stylesPart.GetStream(FileMode.Create, FileAccess.Write)));
    
            }
    
        }
    
    }