替换自定义XML内容 [英] Replacing custom XML content

查看:150
本文介绍了替换自定义XML内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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子文件夹。因此该文件被认为是损坏的。

解决方案

您好,皮埃尔,

不,您不必删除自定义xml部分完成你的方案。相反,您可以打开文档的mainPart,然后遍历所有自定义xml部分。当您找到包含您感兴趣的内容类型的自定义xml部件时,您就知道您有要更新的自定义xml部件。一旦访问了正确的部分,您只需将xml加载为DOM,然后根据需要进行更新。至于你的第二部分,你似乎正在使用System.IO.Packaging和Open XML SDK。我建议只使用Open XML SDK,因为它会适当地处理你的所有关系。无论如何,请按照前几句中概述的步骤完成您的任务。如果您遇到任何其他问题,请告诉我们。

Zeyad Rajabi


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.

解决方案

Hi Pierre,

No, you don't have to delete the custom xml part to accomplish your scenario. Instead, you can open the mainPart of the document and then iterate through all the custom xml parts. When you find a custom xml part that contains the content type that you are interested in then you know you have the custom xml part that you want to update. Once you have access to the right part you can simply load the xml as  DOM and then update as necessary. As for your second part, you seem to be using both System.IO.Packaging and the Open XML SDK. I would suggest to only use the Open XML SDK since it will take care of all your relationships appropriately. In any case, follow the steps I outlined in the first couple of sentences to accomplish your task. let us know if you run into any other issues.

Zeyad Rajabi


这篇关于替换自定义XML内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆