使用包修改Word文档XML

时间:2012-07-11 13:54:15

标签: c# xml ms-word

我正在尝试修改一个简单的MS word模板XML。我意识到有SDK可用,这可以使这个过程更容易,但我负责维护使用包,我被告知也这样做。

我有一个基本的测试文档,其中两个占位符映射到以下XML:

<root>
  <element>
     Fubar
  </element>
  <second>
     This is the second placeholder
  </second>
</root>

我正在做的是使用doc这个词创建一个流,删除现有的XML,获取一些硬编码的测试XML并尝试将其写入流。

以下是我正在使用的代码:

string strRelRoot = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument";
byte[] buffer = File.ReadAllBytes("dev.docx");
//stream with the template
MemoryStream stream = new MemoryStream(buffer, true);
//create a package using the stream
Package package = Package.Open(stream, FileMode.Open, FileAccess.ReadWrite);
PackageRelationshipCollection pkgrcOfficeDocument = package.GetRelationshipsByType(strRelRoot);
foreach (PackageRelationship pkgr in pkgrcOfficeDocument)
{
    if (pkgr.SourceUri.OriginalString == "/")
    {
        Uri uriData = new Uri("/customXML/item1.xml", UriKind.Relative);
        //remove the existing part
        if (package.PartExists(uriData))
        { 
            // Delete template "/customXML/item1.xml" part
            package.DeletePart(uriData);
        }
        //create a new part
        PackagePart pkgprtData = package.CreatePart(uriData, "application/xml");
        //test data
        string xml = @"<root>
                        <element>
                            Changed
                        </element>
                        <second>
                                The second placeholder changed
                        </second>
                    </root>";
        //stream created from the xml string
        MemoryStream fromStream = new MemoryStream();
        UnicodeEncoding uniEncoding = new UnicodeEncoding();
        byte[] fromBuffer = uniEncoding.GetBytes(xml);
        fromStream.Write(fromBuffer, 0, fromBuffer.Length);
        fromStream.Seek(0L, SeekOrigin.Begin);
        Stream toStream = pkgprtData.GetStream();
        //copy the xml to the part stream
        fromStream.CopyTo(toStream);
        //copy part stream to the byte stream
        toStream.CopyTo(stream);

    }
}

虽然我觉得我接近解决方案,但目前还没有修改文档。任何建议将非常感谢。谢谢!

编辑:为了cla,我得到的结果是文件没有变化。我没有例外或类似,但文档XML没有被修改。

1 个答案:

答案 0 :(得分:3)

好的,所以不是我承诺的及时回复,但是这里去了!

问题有几个方面。示例代码来自内存和文档,不一定要编译和测试。


阅读模板XML

在删除包含模板XML的包部分之前,需要打开其流并读取XML。如果部件不存在,那么如何获取XML取决于您。

我的示例代码使用LINQ to XML API中的类,但您可以使用您喜欢的任何一组XML API。

XElement templateXml = null;
using (Stream stream = package.GetPart(uriData))
    templateXml = XElement.Load(stream);
// Now you can delete the part.

此时,您在templateXml中有一个内存中的模板XML表示。


将值替换为占位符

templateXml.SetElementValue("element", "Replacement value of first placeholder");
templateXml.SetElementValue("second", "Replacement value of second placeholder");

如果您需要执行比此更高级的操作,请查看XElement上的方法,例如阅读原始内容以确定替换值。


保存文档

这是您的原始代码,经过修改和注释。

// The very first thing to do is create the Package in a using statement.
// This makes sure it's saved and closed when you're done.
using (Package package = Package.Open(...))
{
    // XML reading, substituting etc. goes here.

    // Eventually...
    //create a new part
    PackagePart pkgprtData = package.CreatePart(uriData, "application/xml");
    // Don't need the test data anymore.
    // Assuming you need UnicodeEncoding, set it up like this.
    var writerSettings = new XmlWriterSettings
    {
        Encoding = Encoding.Unicode,
    };
    // Shouldn't need a MemoryStream at all; write straight to the part stream.
    // Note using statements to ensure streams are flushed and closed.
    using (Stream toStream = pkgprtData.GetStream())
    using (XmlWriter writer = XmlWriter.Create(toStream, writerSettings))
        templateXml.Save(writer);
    // No other copying should be necessary.
    // In particular, your toStream.CopyTo(stream) appeared
    // to be appending the part's data to the package's stream
    // (the physical file), which is a bug.
} // This closes the using statement for the package, which saves the file.