如何将 Xml 更改保存回原始文档

How do I save Xml Changes Back to the Original Document

由于供应商的产品出现问题,我需要更新 MS Word 文档的样式 (styles.xml) 部分。

到目前为止,我已经能够提取和更新我需要的 xml。唯一的问题是,我不知道如何将更改保存回文档。

下面的代码工作正常。我通常将 xml 输出到控制台以确保它正常运行。最后,我知道我需要执行一些保存操作,但是 XDocument.Save( /stream/) 没有工作。

这是我目前的状态

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 

        }

    }

}


我在本页的保存部分部分找到了答案Replace the styles parts in a word processing document (Open XML SDK)

解决方法见代码末尾。您还会看到我尝试过的内容。

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)));

        }

    }

}