这是我的XML文件,这是我从压缩的docx文件中获得的.

这是Docent.xml文件:

<w:document 
xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" 
xmlns:cx="http://schemas.microsoft.com/office/drawing/2014/chartex" 
xmlns:cx1="http://schemas.microsoft.com/office/drawing/2015/9/8/chartex" 
xmlns:cx2="http://schemas.microsoft.com/office/drawing/2015/10/21/chartex" 
xmlns:cx3="http://schemas.microsoft.com/office/drawing/2016/5/9/chartex" 
xmlns:cx4="http://schemas.microsoft.com/office/drawing/2016/5/10/chartex" 
xmlns:cx5="http://schemas.microsoft.com/office/drawing/2016/5/11/chartex" 
xmlns:cx6="http://schemas.microsoft.com/office/drawing/2016/5/12/chartex" 
xmlns:cx7="http://schemas.microsoft.com/office/drawing/2016/5/13/chartex" 
xmlns:cx8="http://schemas.microsoft.com/office/drawing/2016/5/14/chartex" 
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
xmlns:aink="http://schemas.microsoft.com/office/drawing/2016/ink" 
xmlns:am3d="http://schemas.microsoft.com/office/drawing/2017/model3d" 
xmlns:o="urn:schemas-microsoft-com:office:office" 
xmlns:oel="http://schemas.microsoft.com/office/2019/extlst" 
xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" 
xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" 
xmlns:v="urn:schemas-microsoft-com:vml" 
xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" 
xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" 
xmlns:w10="urn:schemas-microsoft-com:office:word" 
xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" 
xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" 
xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" 
xmlns:w16cex="http://schemas.microsoft.com/office/word/2018/wordml/cex" 
xmlns:w16cid="http://schemas.microsoft.com/office/word/2016/wordml/cid" 
xmlns:w16="http://schemas.microsoft.com/office/word/2018/wordml" 
xmlns:w16sdtdh="http://schemas.microsoft.com/office/word/2020/wordml/sdtdatahash" 
xmlns:w16se="http://schemas.microsoft.com/office/word/2015/wordml/symex" 
xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" 
xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" 
xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" 
xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" 
mc:Ignorable="w14 w15 w16se w16cid w16 w16cex w16sdtdh wp14">
<w:body>
<w:p w14:paraId="59E8928B" w14:textId="4F989F00" w:rsidR="00E57A39" w:rsidRPr="008F41DD" w:rsidRDefault="008F41DD" w:rsidP="008F41DD">
<w:pPr>
<w:pStyle w:val="ListParagraph"/>
<w:numPr>
<w:ilvl w:val="0"/>
<w:numId w:val="1"/>
</w:numPr>
<w:rPr>
<w:rFonts w:cs="B Nazanin"/>
</w:rPr>
</w:pPr>
<w:r w:rsidRPr="008F41DD">
<w:rPr>
<w:rFonts w:cs="B Nazanin"/>
</w:rPr>
<w:t xml:space="preserve">One </w:t>
</w:r>
</w:p>
<w:p w14:paraId="2D18B139" w14:textId="40831F20" w:rsidR="008F41DD" w:rsidRPr="008F41DD" w:rsidRDefault="008F41DD" w:rsidP="008F41DD">
<w:pPr>
<w:pStyle w:val="ListParagraph"/>
<w:numPr>
<w:ilvl w:val="0"/>
<w:numId w:val="1"/>
</w:numPr>
<w:rPr>
<w:rFonts w:cs="B Nazanin"/>
</w:rPr>
</w:pPr>
<w:r w:rsidRPr="008F41DD">
<w:rPr>
<w:rFonts w:cs="B Nazanin"/>
</w:rPr>
<w:t>Two</w:t>
</w:r>
</w:p>
<w:p w14:paraId="4E1C2299" w14:textId="45351C9E" w:rsidR="008F41DD" w:rsidRPr="008F41DD" w:rsidRDefault="008F41DD" w:rsidP="008F41DD">
<w:pPr>
<w:pStyle w:val="ListParagraph"/>
<w:numPr>
<w:ilvl w:val="0"/>
<w:numId w:val="1"/>
</w:numPr>
<w:rPr>
<w:rFonts w:cs="B Nazanin"/>
</w:rPr>
</w:pPr>
<w:r w:rsidRPr="008F41DD">
<w:rPr>
<w:rFonts w:cs="B Nazanin"/>
</w:rPr>
<w:t>Three</w:t>
</w:r>
</w:p>
<w:p w14:paraId="07C809C6" w14:textId="10C92B2E" w:rsidR="008F41DD" w:rsidRPr="008F41DD" w:rsidRDefault="008F41DD" w:rsidP="008F41DD">
<w:pPr>
<w:pStyle w:val="ListParagraph"/>
<w:numPr>
<w:ilvl w:val="0"/>
<w:numId w:val="1"/>
</w:numPr>
<w:rPr>
<w:rFonts w:cs="B Nazanin"/>
</w:rPr>
</w:pPr>
<w:r w:rsidRPr="008F41DD">
<w:rPr>
<w:rFonts w:cs="B Nazanin"/>
</w:rPr>
<w:t>Four</w:t>
</w:r>
</w:p>
<w:p w14:paraId="2DB3ADA1" w14:textId="2F165FEA" w:rsidR="008F41DD" w:rsidRPr="008F41DD" w:rsidRDefault="008F41DD" w:rsidP="008F41DD">
<w:pPr>
<w:rPr>
<w:rFonts w:cs="B Nazanin"/>
</w:rPr>
</w:pPr>
</w:p>
<w:p w14:paraId="086A6010" w14:textId="77777777" w:rsidR="008F41DD" w:rsidRPr="008F41DD" w:rsidRDefault="008F41DD" w:rsidP="008F41DD">
<w:pPr>
<w:rPr>
<w:rFonts w:cs="B Nazanin"/>
</w:rPr>
</w:pPr>
</w:p>
<w:sectPr w:rsidR="008F41DD" w:rsidRPr="008F41DD">
<w:pgSz w:w="12240" w:h="15840"/>
<w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="720" w:footer="720" w:gutter="0"/>
<w:cols w:space="720"/>
<w:docGrid w:linePitch="360"/>
</w:sectPr>
</w:body>
</w:document>

我的项目终结点API和代码:

[HttpPost("[action]")]
[Consumes("application/xml")]
public IActionResult UploadXml([FromBody] XElement xml)
{
    MemoryStream generatedDocument = new MemoryStream();

    using (WordprocessingDocument package = WordprocessingDocument.Create(generatedDocument, WordprocessingDocumentType.Document))
    {
        var mainPart = package.AddMainDocumentPart();

        var doc = new XmlDocument();
        doc.LoadXml(xml.ToString());

        new Document(doc.OuterXml);
        package.Save();
    }

    var result = generatedDocument.ToArray();

    return File(result, "application/octet-stream", "document.docx");
}

现在,我想将所有这些XML作为字符串传递给OpenXMLSDK包,但我不知道如何传递.

我搜索了很多,但我不能解决这个问题,我需要一个示例代码来告诉我如何做到这一点.

如果有人以前这样做过,请与我分享您的代码.

我想说,我知道有太多的文档,但我应该快点做,我不能读微软糟糕的文档.

谢谢

推荐答案

首先,您所创建的Document实例在您所显示的代码中不会出现.如果不将其添加到包中,则文档中将没有任何内容.

以下是进行了所需更改的工作代码,基本上消除了不必要的XmlDocument操作和不必要的字节数组创建,因为文件确实接受流.

[HttpPost()]
[Consumes("application/xml")]
public IActionResult UploadXml([FromBody] XElement xml)
{
    MemoryStream generatedDocument = new MemoryStream();

    using (WordprocessingDocument package = WordprocessingDocument.Create(generatedDocument, WordprocessingDocumentType.Document))
    {
        var mainPart = package.AddMainDocumentPart();

        mainPart.Document = new Document(xml.ToString(SaveOptions.DisableFormatting));
        package.Save();
    }

    generatedDocument.Position = 0; // to start of stream!

    return File(generatedDocument, "application/octet-stream", "document.docx");
}

我假设您知道,如果您希望操作接受.Net Core中的XElement,则必须如下所示启用XML序列化程序:

public void ConfigureServices(IServiceCollection services)
{
    services
       .AddControllersWithViews( opt => { opt.MaxValidationDepth = 64; })
       .AddXmlSerializerFormatters();
}

我还必须将MaxValidationDepth设置为比缺省值32更高的值.倍增对示例数据有效.

为了完整起见,下面是我的curl测试命令

curl --trace con -H"Content-Type: application/xml" -d@worddoc.xml  --output doctest.docx  http://localhost:5000/Home/UploadXml

这就是我最终得到的结论:

enter image description here

Csharp相关问答推荐

如何使用CsvReader获取给定列索引的列标题?

Elasticsearch:当我try 使用c#将嵌套对象添加到filter中时出现问题

使用C#中的SDK在Microsoft Graph API上使用SubscribedSkus的问题

为什么无法将对象转换为泛型类型

在.NET 7.0 API控制器项目中通过继承和显式调用基类使用依赖项注入

什么时候接受(等待)信号灯?尽可能的本地化?

如何在C#中转换泛型包装类内部的派生类

带有可选参数的模拟方法返回意外的不同值,具体取决于可选的默认值

无法使用[FromForm]发送带有图像和JSON的多部分请求

在使用UserManager时,如何包含与其他实体的关系?

有条件地定义预处理器指令常量

CRL已过期,但ChainStatus告诉我RevocationStatus未知

MSI无法将快捷方式添加到启动文件夹

使用ASP.NET MVC for Lemon Squeezy X-Signature创建散列

在C#中,当输入一个方法/局部函数时,我的IEnumerator被重置/重新创建.为什么?

岛屿和框架中的自定义控件库.Navigate-AccessViolationException

如何从SignalR获取连接客户端的域

Autofac -动态实例化:手动传递构造函数

为什么连接到Google OAuth2后,结果.Credential为空?

无法创建&Quot;&Quot;类型的实例,因为无法将一个或多个参数绑定到