问题描述
Hello All,
我使用Word 2007内容控制工具包在DOCX包中创建了自定义XML部分,并希望通过
.Net web以编程方式C#中的应用程序,用于更改与word 2007内容控件关联的自定义XML内容部分。
准备工作是使用此处描述的技术和工具完成的。
http://blogs.msdn.com/brian_jones/archive/2009/01/05/taking-advantage-of-bound-content-controls.aspx
然后我使用以下代码查看会发生什么:
protected void ReplaceCustomXML(string fileName, string customXML)
{
using(WordprocessingDocument wordDoc = WordprocessingDocument.Open(fileName,true))
{
MainDocumentPart mainPart = wordDoc.MainDocumentPart;结果,mainPart.DeleteParts< CustomXmlPart>(mainPart.CustomXmlParts);
//添加新的customXML部件,然后添加内容
CustomXmlPart customXmlPart = mainPart.AddNewPart< CustomXmlPart>();
//将XML复制到新部件中...
使用(StreamWriter ts = new StreamWriter(customXmlPart.GetStream()))
ts.Write(customXML) ;
}
代码部分工作在于,当我打开DOCX文档时,我的内容控件确实包含新的XML内容,但DOCX不再可用于Word 2007内容控制工具包。从zip查看器查看包时,显示基本上是一个损坏的包。
为什么使用OPen XML SDK 1来简单地更改自定义xml数据这么复杂。唯一的选择是删除部件并重新创建。我不希望改变它的XML模式; s内容是programmaticaly。
第二部分
我使用下面的代码管理来替换customXml部分
//使用System.Xml的
;使用DocumentFormat.OpenXml.Packaging;
使用System.IO;
//使用WIndowsbase.dll
System.IO.Packaging程序;
public partial class _Default:System.Web.UI.Page
{
protected void Page_Load(object sender,EventArgs e)
{
const string sFileName = @" N:\ PersersDevel \ Open XML\Word2007ContentControlToolkit_SampleDocuments_v1\Word2007ContentControlToolkit_SampleDocuments_v1 \Simple\EasyOrder.docx" ;;
const string sSourceXML = @" N:\ PersonalDevel \ Open XML \ DummyForm.xml" ;;
使用(WordprocessingDocument wdDoc = WordprocessingDocument.Open(sFileName,true))
MainDocumentPart abc = wdDoc.MainDocumentPart;
使用(Package package = Package.Open(sFileName,FileMode.Open,FileAccess.ReadWrite))
item1.xml",UriKind.Relative);
package.DeletePart(uriPartTarget);
//重新创建新文档部件
PackagePart packagePartReplacement = package.CreatePart(uriPartTarget," application / vnd.openxmlformats-officedocument.customXmlProperties + xml" ;);
using(FileStream fileStream = new FileStream(sSourceXML,FileMode.Open,FileAccess.Read))
{
//加载新的自定义。 xml使用流。
CopyStream(fileStream,packagePartReplacement.GetStream());
}
}
}
private static void CopyStream(Stream source,Stream target)
{
const int bufSize = 0x1000;
// const int bufSize = 1024;
byte [] buf = new byte [bufSize];
int bytesRead = 0;
while( (bytesRead = source.Read(buf,0,bufSize))> 0)
{
target.Write(buf,0,(int)bytesRead);
}
source.Close();
target.Close();
}
}
但是我丢失了_rels子文件夹。因此该文件被认为是损坏的。
Hello All,
I have created a custom XML section in a DOCX package using the Word 2007 Content Control Toolkit and wish to programmatically via a
.Net web application in C# to change the custom XML content part associated with word 2007 content controls.
The prep work was done using the technic and tools described here.
http://blogs.msdn.com/brian_jones/archive/2009/01/05/taking-advantage-of-bound-content-controls.aspx
Then I used the following code to see what happens:
protected void ReplaceCustomXML(string fileName, string customXML)
{
using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(fileName, true))
{
MainDocumentPart mainPart = wordDoc.MainDocumentPart;
mainPart.DeleteParts<CustomXmlPart>(mainPart.CustomXmlParts);
//Add a new customXML part and then add content
CustomXmlPart customXmlPart = mainPart.AddNewPart<CustomXmlPart>();
//copy the XML into the new part...
using (StreamWriter ts = new StreamWriter(customXmlPart.GetStream()))
ts.Write(customXML);
}
}
The code partially works in that when I open the DOCX document my content control does contain the new XML content but the DOCX is no longer usable with the Word 2007 Content Control Toolkit. When looking at the package from a zip viewer reveals basically a broken package.
Why is it so complicated using the OPen XML SDK 1 to simply change custom xml data. Is the only option is to delete the part and recreate. I do not wish to change the XML schema just it;s content programmaticaly.
Second Part
I have managed using the code below to replace the customXml part
//Required for
using System.Xml;
using DocumentFormat.OpenXml.Packaging ;
using System.IO;
//WIndowsbase.dll
using System.IO.Packaging;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
const string sFileName = @"N:\PersonalDevel\Open XML\Word2007ContentControlToolkit_SampleDocuments_v1\Word2007ContentControlToolkit_SampleDocuments_v1\Simple\EasyOrder.docx";
const string sSourceXML = @"N:\PersonalDevel\Open XML\DummyForm.xml";
using (WordprocessingDocument wdDoc = WordprocessingDocument.Open(sFileName, true))
{
MainDocumentPart abc = wdDoc.MainDocumentPart;
}
using (Package package = Package.Open(sFileName, FileMode.Open, FileAccess.ReadWrite))
{
Uri uriPartTarget = new Uri("/customXml/item1.xml", UriKind.Relative);
package.DeletePart(uriPartTarget);
// Recreate a new document part
PackagePart packagePartReplacement = package.CreatePart(uriPartTarget, "application/vnd.openxmlformats-officedocument.customXmlProperties+xml");
using (FileStream fileStream = new FileStream(sSourceXML, FileMode.Open, FileAccess.Read))
{
// Load the new custom.xml using a stream.
CopyStream(fileStream, packagePartReplacement.GetStream());
}
}
}
private static void CopyStream(Stream source, Stream target)
{
const int bufSize = 0x1000;
// const int bufSize = 1024;
byte[] buf = new byte[bufSize];
int bytesRead = 0;
while ((bytesRead = source.Read(buf, 0, bufSize)) > 0)
{
target.Write(buf, 0, (int)bytesRead);
}
source.Close();
target.Close();
}
}
However I lost the _rels sub folder. Thus the file is considered corrupt.
这篇关于替换自定义XML内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!