简体   繁体   中英

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?). So I've been given the task to work with xml files that look something like this:

--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.

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. Also I'm currently just testing this out in a c# project.

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:

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();
                }
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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