如何在c#

时间:2019-06-27 08:23:25

标签: c# soap .net-core mtom

我想解析肥皂味MTOM格式的Web服务响应。 是回应

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

我尝试使用XmlDictionaryReader,但它给出了错误 “此平台不支持消息传输优化机制(MTOM)消息编码。”

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

我想从soap元素中提取文件名。如何获取?

2 个答案:

答案 0 :(得分:1)

我终于可以使用Regex正常工作了。丢失了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
    {
    }
}

答案 1 :(得分:0)

使用VS(编辑/粘贴为json或xml)或enter link description here

解析xml结构。
     [XmlRoot(ElementName = "output", Namespace = "http://org/emedny/fts/")]
        public class Output
        {
            [XmlElement(ElementName = "fileName", Namespace = "http://org/emedny/fts/")]
            public string FileName { get; set; }
        }

        [XmlRoot(ElementName = "sendDataResponse", Namespace = "http://org/emedny/fts/")]
        public class SendDataResponse
        {
            [XmlElement(ElementName = "output", Namespace = "http://org/emedny/fts/")]
            public Output Output { get; set; }
            [XmlAttribute(AttributeName = "a", Namespace = "http://www.w3.org/2000/xmlns/")]
            public string A { get; set; }
            [XmlAttribute(AttributeName = "xmlns")]
            public string Xmlns { get; set; }
        }

        [XmlRoot(ElementName = "Body", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
        public class Body
        {
            [XmlElement(ElementName = "sendDataResponse", Namespace = "http://org/emedny/fts/")]
            public SendDataResponse SendDataResponse { get; set; }
        }

        [XmlRoot(ElementName = "Envelope", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
        public class Envelope
        {
            [XmlElement(ElementName = "Body", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
            public Body Body { get; set; }
            [XmlAttribute(AttributeName = "soapenv", Namespace = "http://www.w3.org/2000/xmlns/")]
            public string Soapenv { get; set; }
        }


.....


using (var st = File.OpenRead(@"i_saved_your_request_here.xml"))
{
    var data = new XmlSerializer(typeof(Envelope)).Deserialize(st) as Envelope;
    var output = data.Body.SendDataResponse.Output.FileName;
}