简体   繁体   English

如何使用C#覆盖DOCX XML文件?

[英]How do I overwrite a DOCX XML file with C#?

I'm stuck. 我被卡住了。 The goal is to change reviewer names in MS Word track changes using a C# tool, only if the corresponding date attribute is after a given date. 目标是仅当相应的date属性在给定日期之后,才使用C#工具更改MS Word跟踪更改中的审阅者姓名。 The variable string docPath is passed to the method changeRevAuthor . 变量字符串docPath传递给方法changeRevAuthor docPath represents the opened DOCX file. docPath代表打开的DOCX文件。 The file MUST be opened when it is passed because the tool also upsaves all DOC files to DOCX before running the method. 传递文件时必须将其打开,因为该工具还会在运行该方法之前将所有DOC文件保存为DOCX。

The author names ARE changed and the date is confirmed correctly in the XML file. 作者姓名被更改,并且XML文件中的日期已正确确认。 We tested this by saving the XML to a static path. 我们通过将XML保存到静态路径进行了测试。 However, what I need to do is actually overwrite the existing XML file, instead of pulling it out and saving it somewhere else. 但是,我需要做的实际上是覆盖现有的XML文件,而不是将其拉出并保存到其他位置。 Here is the existing method: 这是现有方法:

    private void changeRevAuthor(string docPath)
    {
        using (Stream stream = System.IO.File.Open(docPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
        {  
            stream.Seek(0, SeekOrigin.End); 
            XNamespace w = "http://schemas.openxmlformats.org/wordprocessingml/2006/main";
            WordprocessingDocument document = WordprocessingDocument.Open(stream, false);
            XDocument mainDocumentXDoc = document.MainDocumentPart.GetXDocument();          
            var nodes = mainDocumentXDoc.Descendants().Where(x => x.Attributes(w + "author").Count() > 0);
            var currentdate = "2016-06-21";

            foreach (XElement node in nodes)
            {
                var date = node.Attribute(w + "date").ToString().Substring(8, 10);
                if (DateTime.Parse(date) < DateTime.Parse(currentdate))
                {
                    node.SetAttributeValue(w + "author", "LUZ");
                    Debug.WriteLine(date);
                }                   
            }
            mainDocumentXDoc.Save(document.MainDocumentPart.GetStream(FileMode.Create));

        }
    }

From research, I believe the final line should do the trick: mainDocumentXDoc.Save(document.MainDocumentPart.GetStream(FileMode.Create)); 通过研究,我相信最后一行应该可以解决问题: mainDocumentXDoc.Save(document.MainDocumentPart.GetStream(FileMode.Create));

However, this returns the following error: 但是,这将返回以下错误:

'An unhandled exception of type 'System.IO.IOException' occurred in System.IO.Packaging.dll Additional information: Cannot get stream with FileMode.Create, FileMode.CreateNew, FileMode.Truncate, FileMode.Append when access is FileAccess.Read.' 在System.IO.Packaging.dll中发生“类型为'System.IO.IOException的未处理的异常'。 。”

When I change it to FileAccess.Write or FileAccess.ReadWrite , I receive the following error: 当我将其更改为FileAccess.WriteFileAccess.ReadWrite ,收到以下错误:

'Additional information: The process cannot access the file 'C:\\Users....\\Desktop\\C# Test\\Results\\newtest.docx' because it is being used by another process.' '其他信息:该进程无法访问文件'C:\\ Users .... \\ Desktop \\ C#Test \\ Results \\ newtest.docx',因为该文件正在被另一个进程使用。'

Where do I go from here? 我从这里去哪里?

Thanks Luke 谢谢卢克

This solves your issue: 这可以解决您的问题:

void changeRevAuthor (string docPath)
{
  XNamespace w = "http://schemas.openxmlformats.org/wordprocessingml/2006/main";
  WordprocessingDocument document = WordprocessingDocument.Open(docPath, true);
  XDocument mainDocumentXDoc = document.MainDocumentPart.GetXDocument();

  var nodes = mainDocumentXDoc.Descendants().Where(x => x.Attributes(w + "author").Count() > 0);

  foreach (XElement node in nodes)
  {
    // ...
    node.SetAttributeValue(w + "author", "LUZ");        
  }
  mainDocumentXDoc.Save(document.MainDocumentPart.GetStream(FileMode.Create));
}

but is incomplete . 不完整

You should also modify word\\comments and docProps\\core package parts, not only word\\document . 您还应该修改word \\ commentsdocProps \\ core软件包部分,而不仅限于word \\ document

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM