2015-10-31 3 views
0

У меня есть веб-сервис, с методом и консольного приложения, где я использовал веб-службыКак десериализовать xmlResult из WebService или использовать webService без SOAP?

[WebMethod] 
public Foo HelloWorld(Foo foo) 
{ 
    Foo foo2 = new Foo(); 
    foo2.Id = 10; 
    foo2.Name = "roman"; 
    return foo2; 
} 

и приложение, в котором я использовал этот веб-метод:

using (WebClient client = new WebClient()) 
{ 
    { 
     //how to replace this block on a short line likes Foo result = webService.WebMethod(params) 
     client.Headers.Add("SOAPAction","\"http://tempuri.org/HelloWorld\""); 

     client.Headers.Add("Content-Type", "text/xml; charset=utf-8"); 
     string payload = @"<?xml version=""1.0"" encoding=""utf-8""?> 
          <soap:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" 
              xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" 
              xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/""> 
           <soap:Body> 
            <HelloWorld xmlns=""http://tempuri.org/""> 
             <foo> 
              <Id>10</Id> 
              <Name>23</Name> 
             </foo> 
            </HelloWorld> 
           </soap:Body> 
          </soap:Envelope>"; 

     var data = Encoding.UTF8.GetBytes(payload); 
     var result = client.UploadData("http://localhost:22123/Service1.asmx", data); 
     Console.WriteLine(Encoding.Default.GetString(result)); 
    } 
} 

Результат является:

<?xml version="1.0" encoding="utf-8"?> 
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <soap:Body> 
     <HelloWorldResponse xmlns="http://tempuri.org/"> 
      <HelloWorldResult> 
       <Id>10</Id> 
       <Name>roman</Name> 
      </HelloWorldResult> 
     </HelloWorldResponse> 
    </soap:Body> 
</soap:Envelope> 

Мои проблемы:

  1. Как заменить много строк кода на маленькую строку?
  2. Как десериализовать webResult с помощью Linq для XML или есть другой способ сделать это?
+0

Возможный дубликат [How для десериализации документа XML] (http://stackoverflow.com/questions/364253/how-to-deserialize-xml-document) – CSharpie

+0

Отчасти вы правы, но, к сожалению, нет. Мне нужно отправить SOAP в веб-службу, получить SOAP из веб-службы и разобрать результат на класс. Я хочу знать, как это сделать без создания SOAP (myclass result = myWebService.WebMethod (некоторые переменные). –

ответ

0

Я знаю, что это не является хорошим решением, но я создать общие методы для создания SOAP из разных классов

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using ConsoleApplication1.ServiceReference1; 
using System.Net; 
using System.Xml.Serialization; 
using System.IO; 
using System.Runtime.Serialization.Formatters.Soap; 
using System.Text.RegularExpressions; 

namespace ConsoleApplication1 
{ 
    [Serializable] 
    public class Foo 
    { 
     public Foo() { } 
     public Foo(int id, string name) 
     { 
      Id = id; 
      Name=name; 
     } 
     public int Id { get; set; } 
     public string Name { get; set; } 
    } 

    [Serializable] 
    public class Roman 
    { 
     public Roman() { } 
     public Roman(int id, string bushuev, string somethingElse) 
     { 
      Id = id; 
      Bushuev = bushuev; 
      SomethingElse = somethingElse; 
     } 
     public int Id { get; set; } 
     public string Bushuev { get; set; } 
     public string SomethingElse { get; set; } 
    } 

    class Program 
    { 
     public static Stream GenerateStreamFromString(string s) 
     { 
      MemoryStream stream = new MemoryStream(); 
      StreamWriter writer = new StreamWriter(stream); 
      writer.Write(s); 
      writer.Flush(); 
      stream.Position = 0; 
      return stream; 
     } 
     public static string GeneralSerilize<T>(T input) 
     { 
      string result=null; 
      XmlSerializer xmlSerializer = 
       new XmlSerializer(input.GetType()); 
      using (StringWriter textWriter = new StringWriter()) 
      { 
       xmlSerializer.Serialize(textWriter, input); 
       result = textWriter.ToString(); 
       string pattern = string.Format("(?<first><{0}(.|\r|\n)+{0}>)",input.GetType().Name); 
       Match match = Regex.Match(result, pattern); 
       result = match.Value; 
       result = result.Replace(
        @"xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema""", 
        string.Empty); 

       result = result.Replace(input.GetType().Name, 
            input.GetType().Name.ToLower()); 
      } 
      return result; 
     } 
     public static T GeneralDeserialize<T>(string input) where T:class 
     { 
      T result = null; 
      XmlSerializer xml = new XmlSerializer(typeof(T)); 

      { 
       string inptuResult = input; 
       string pattern = string.Format("(?<first><{0}(.|\r|\n)+{0}>)", typeof(T).Name); 
       Match match = Regex.Match(input, pattern); 
       input = match.Value; 
      } 

      using (Stream stream = GenerateStreamFromString(input)) 
      { 
       result = (T)xml.Deserialize(stream); 
      } 

      return result; 
     } 

     public static T UseWEbService<T>(T input, string nameMethod) where T:class 
     { 
      string xmlResult = GeneralSerilize<T>(input); 

      using (WebClient client = new WebClient()) 
      { 
       try 
       { 
        string stringAction = string.Format("\"http://tempuri.org/{0}\"",nameMethod); 
        client.Headers.Add("SOAPAction", stringAction); 

        client.Headers.Add("Content-Type", "text/xml; charset=utf-8"); 
        string payload= string.Empty; 
        string begin = @"<?xml version=""1.0"" encoding=""utf-8""?> 
            <soap:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" 
                xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" 
                xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/""> 
             <soap:Body>"; 

        string end [email protected]"</soap:Body> 
            </soap:Envelope>"; 

        string beginMethod = string.Empty; 
        beginMethod = string.Format(@"<{0} xmlns=""http://tempuri.org/"">",nameMethod);//"<" + nameMethod + ">"; 

        string endMethod = string.Empty; 
        endMethod = "</" + nameMethod + ">"; 
        payload = begin + 
         beginMethod+ 
         xmlResult+ 
         endMethod 
         + end; 

        byte[] data = Encoding.UTF8.GetBytes(payload); 
        byte[] webServiceResponse = client.UploadData("http://localhost:22123/Service1.asmx", data); 
        string stringResponse = Encoding.Default.GetString(webServiceResponse); 

        try 
        { 
         string inputXML = stringResponse.Replace(nameMethod + "Result", input.GetType().Name); 
         T resultMethod = GeneralDeserialize<T>(inputXML); 
         return resultMethod; 
        } 

        catch (Exception ex) 
        { 
         Console.WriteLine(ex.Message); 
        } 
       } 

       catch (Exception ex) 
       { 
        Console.WriteLine(ex.Message); 
       } 
      } 
      return null; 
     } 
     static void Main(string[] args) 
     { 
      Foo foo = UseWEbService < Foo>(new Foo(10, "roman"), "HelloWorld"); 
      Roman roman = new Roman(123, 
       "roman ", "bushuev"); 
      Roman romanResult = UseWEbService<Roman>(roman, "HelloRoman");   
     } 
    } 
} 

Чтобы найти лучшее решение, вам необходимо прочитать Troelsen C# 2010 .NET 4 Введение в Windows Communication Foundation

+0

Лучшим способом является использование WCF –