简体   繁体   English

从 MIME 多部分文件中提取 XML object

[英]Extracting XML object from MIME Multipart file

So I'm in a bit of a cross road here trying to consume the data in files that from my understanding (after reading up a bit) seems to be multipart soap requests (with the xml object as an attachment?).因此,我在这里尝试使用文件中的数据,据我了解(在阅读了一点之后)似乎是多部分 soap 请求(使用 xml ZA8CFDE6331BD49EB62AC96F891 作为附件?)。 So I've been given the task to work with xml files that look something like this:所以我被赋予了使用 xml 文件的任务,这些文件看起来像这样:

--MIME264440613829.7322990959788848043325807015

<SOAP-ENV:Envelope>
  <SOAP-ENV:Header>
    ...
  </SOAP-ENV:Header>
  <SOAP-ENV:Body>
    <eb:Manifest eb:version="2.0">
      <eb:Reference xlink:href="cid:payload-1" xlink:role="aop:ROOT"/>
    </eb:Manifest>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

--MIME264440613829.7322990959788848043325807015
Content-ID: payload-1

<?xml version="1.0" encoding="UTF-8"?>
<aop:ROOT>
...
</aop:ROOT>
--MIME264440613829.7322990959788848043325807015--

And what i need to do is to extract the XML in what seems to be the second part (or the attachment as i think people call it) from the multipart object.我需要做的是从多部分 object 中提取 XML 似乎是第二部分(或我认为人们称之为附件)。

My first thought was to just use some string operations with for example regex to extract the xml object, but surely there must be a better way.我的第一个想法是使用一些字符串操作,例如正则表达式来提取 xml object,但肯定有更好的方法。 Also I'm currently just testing this out in a c# project.另外,我目前只是在 c# 项目中对此进行测试。

Ok, So after contacting one of my colleagues we managed to find a solution:好的,所以在联系了我的一位同事后,我们设法找到了解决方案:

So this is what we did这就是我们所做的

Using HttpMultiPartParser we could extract the xml content from the file by the code below:使用 HttpMultiPartParser 我们可以通过以下代码从文件中提取 xml 内容:

using HttpMultipartParser;      

        using (var streamReader = new StreamReader(@"multipartFile.xml"))
        {
            var temp = MultipartFormDataParser.Parse(streamReader.BaseStream);
            foreach (var filepart in temp.Files)
            {
                using (var fpSr = new StreamReader(filepart.Data))
                {
                    var name = filepart.Name;
                    var contentType = filepart.ContentType;
                    var content = fpSr.ReadToEnd();
                }
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM