Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在处理字节和内存流时,如何省略WordML document.xml中的xml声明?_C#_Openxml Sdk_Wordml - Fatal编程技术网

C# 在处理字节和内存流时,如何省略WordML document.xml中的xml声明?

C# 在处理字节和内存流时,如何省略WordML document.xml中的xml声明?,c#,openxml-sdk,wordml,C#,Openxml Sdk,Wordml,我有两段不同的代码 using (WordprocessingDocument wordDocument = WordprocessingDocument.Create(@"D:\Tests\WordML\ML_Example.docx", WordprocessingDocumentType.Document)) { // Add a main document part. MainDocumentPart

我有两段不同的代码

using (WordprocessingDocument wordDocument =
           WordprocessingDocument.Create(@"D:\Tests\WordML\ML_Example.docx", WordprocessingDocumentType.Document))
        {
            // Add a main document part. 
            MainDocumentPart mainPart = wordDocument.AddMainDocumentPart();
            TextReader tr = new StreamReader("SimpleTextExample.xml");
            mainPart.Document = new Document(tr.ReadToEnd());
        }
在这里,它工作得很好,并生成.docx

现在,第二种方法是使用字节和内存流uri

 MemoryStream modeleRootStream = new MemoryStream();
XmlWriterSettings writerSettings=新的XmlWriterSettings { OmitXmlDeclaration=true }; XmlWriter=XmlWriter.Create(modeleRootStream,writerSettings)

当您使用第二种方式时,生成的文档不好。在document.xml中,您将看到xml声明。initialXml表示初始WordML xml的元素

<?xml version="1.0" encoding="utf-8"?><w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">

当你试图用Word打开它时,它会告诉你文件被截断了

如何使用字节和内存流而不出现此问题。

这是错误的:

byte[] wordDocBytes = streamWithWord.GetBuffer();
这应该是:

byte[] wordDocBytes = streamWithWord.ToArray();
GetBuffer
返回缓冲区的额外部分,而不仅仅是有效数据

通常可以使用
XmlWriterSettings
忽略xml头

几乎可以肯定的是,写得更直接,但我的信号即将用完(在火车上…。

这是错误的:

byte[] wordDocBytes = streamWithWord.GetBuffer();
这应该是:

byte[] wordDocBytes = streamWithWord.ToArray();
GetBuffer
返回缓冲区的额外部分,而不仅仅是有效数据

通常可以使用
XmlWriterSettings
忽略xml头

几乎可以肯定的是,写得更直接,但我的信号(在火车上…)快用完了