Search code examples
c#soap.net-coremtom

How to parse MTOM soap response in c#


I want to parse the web service response which is in soap MTOM format. It is the response

--MIMEBoundary_437235bf25f089af96e3a1387b60bdfbd52e58e56b20f9b0
Content - Type: application / xop + xml; charset = utf - 8; type = "text/xml"
Content - Transfer - Encoding: binary
Content - ID: < 0.737235bf25f089af96e3a1387b60bdfbd52e58e56b20f9b0 @apache.org >

    <? xml version = "1.0" encoding = "utf-8" ?>
    < soapenv : Envelope xmlns: soapenv = "http://schemas.xmlsoap.org/soap/envelope/" >
    < soapenv:Body >
    < sendDataResponse xmlns: a = "http://www.w3.org/2005/05/xmlmime" xmlns = "http://org/emedny/fts/" >
    < output >< fileName > ABC.x12 </ fileName ></ output >
    </ sendDataResponse >
    </ soapenv:Body >
    </ soapenv:Envelope >

    --MIMEBoundary_437235bf25f089af96e3a1387b60bdfbd52e58e56b20f9b0--

i have tried using XmlDictionaryReader but it is giving error "The Message Transmission Optimization Mechanism (MTOM) message encoding is not supported on this platform."

string MTOM = response;

MemoryStream ms;
ItemOperations obj;
DataContractSerializer dcs = new DataContractSerializer(typeof(ItemOperations));

string fixedMtom = MTOM.Replace(
    "Multipart/Related;boundary=437235bf25f089af96e3a1387b60bdfbd52e58e56b20f9b0;",
    "Multipart/Related;boundary=\"437235bf25f089af96e3a1387b60bdfbd52e58e56b20f9b0?MTOM\";");
ms = new MemoryStream(Encoding.UTF8.GetBytes(fixedMtom));
XmlDictionaryReader reader = XmlDictionaryReader.CreateMtomReader(ms, Encoding.UTF8, XmlDictionaryReaderQuotas.Max);

I want to extract the file name from soap element.How can i get it?


Solution

  • I finally got it to work using Regex. Lost of issues with space in the xml :

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml;
    using System.Xml.Linq;
    using System.IO;
    using System.Text.RegularExpressions;
    
    namespace ConsoleApplication117
    {
        class Program
        {
            const string FILENAME = @"c:\temp\test.txt";
            static void Main(string[] args)
            {
                string input = File.ReadAllText(FILENAME);
                string pattern = @"(?'xml'\<\?.*)--MIMEBoundary";
    
                Match match = Regex.Match(input,pattern,RegexOptions.Singleline);
                string xml = match.Groups["xml"].Value.Trim();
    
                string pattern1 = @"\<[?/]?((\s*\w+\s*:\s*\w+)|(\s*\w+))";
                xml = Regex.Replace(xml, pattern1, ReplaceSpaces);
                string pattern2 = @"\w+\s*:\s*\w+\s*=";
                xml = Regex.Replace(xml, pattern2, ReplaceSpaces); //remove spaces in namespaces
    
                XDocument doc = XDocument.Parse(xml);
                string filename = (string)doc.Descendants().Where(x => x.Name.LocalName == "fileName").FirstOrDefault();
            }
            static string ReplaceSpaces(Match match)
            {
                string input = match.Value;
                string pattern = @"\s*(?'name1'\w+)\s*(?'colon':)*\s*(?'name2'\w*)";
                string output = Regex.Replace(input, pattern, "${name1}${colon}${name2}");
                return output;
            }
        }
        public class Player
        {
        }
    }