【问题标题】:How do I save Xml Changes Back to the Original Document如何将 Xml 更改保存回原始文档
【发布时间】:2019-09-16 04:34:40
【问题描述】:

由于供应商产品的问题,我需要更新 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 

        }

    }

}


【问题讨论】:

    标签: ms-word linq-to-xml openxml openxml-sdk


    【解决方案1】:

    我在本页的保存零件部分找到了答案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)));
    
            }
    
        }
    
    }
    
    

    【讨论】:

      猜你喜欢
      • 2010-12-15
      • 2016-09-29
      • 2020-08-21
      • 2011-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-16
      • 1970-01-01
      相关资源
      最近更新 更多