2012-05-22 2 views
1

ASP.NET MVC C# 웹 응용 프로그램 내에서 읽어야하는 XML SOAP 메시지가 원격으로 호스팅되어 있습니다. 나는 위의 모든 기술에 익숙하지 않으므로 제발 쉽게 가십시오.SOAP 메시지 응답 읽기 ASP.NET MVC4 C#

  1. 어떻게
  2. 가 어떻게 SOAP 메시지를 모델링하는 모델을 만들려면 어떻게해야합니까 데이터 소스에 연결합니까
  3. 내가이에 "GetMetalQuoteResult"의 내용을 얻을 설정해야 할 것 LINQ 쿼리는 C# 개체? 예 : 비누 메시지 응답의 개별 요소에 대한 액세스 권한을 얻습니다.

아래의 스키마.

<?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> 
     <GetMetalQuoteResponse xmlns="http://.../..."> 
      <GetMetalQuoteResult> 
       <Type>string</Type> 
       <Currency>string</Currency> 
       <Date>Date</Date> 
       <Time>Time</Time> 
       <Rate>decimal</Rate> 
       <Bid>decimal</Bid> 
       <BidTime>Time</BidTime> 
       <ExpTime>DateTime</ExpTime> 
       <DisplayTime>Time</DisplayTime> 
       <DisplayDate>Date</DisplayDate> 
       <Ask>Decimal</Ask> 
       <AskTime>Time</AskTime> 
      </GetMetalQuoteResult> 
     </GetMetalQuoteResponse> 
    </soap:Body> 
</soap:Envelope> 

현재 내 컨트롤러에는 다음 코드가 있습니다.

var xml = XElement.Load(url); 
System.Diagnostics.Debug.WriteLine(""); 
foreach (XElement x in xml.Nodes()) 
{ 
    System.Diagnostics.Debug.WriteLine(x.Name + ":\n"+ x.Value); 
} 
System.Diagnostics.Debug.WriteLine(""); 

그러나 이것은 단지 다음 반환

{http://schemas.xmlsoap.org/soap/envelope/} 
Body:XAUGBP5/22/201212:21:04 PM1000.86251000.862512:21:04 PM2012 May 22 12:21 PM BST1:21:04 PM EDT05/22/121001.249412:21:04 PM 

나는 그것이 별도의 줄에 반환해야합니다

Type: XAU 
Currency: GBP 
Date: 5/22/201212:21:04 
.... 
.... 

감사를 사전에 도움을.

답변

2

클라이언트 프록시 클래스를 생성하는 것이 좋습니다. Visual Studio 내에서 서비스 참조를 추가하거나 wsdl.exe 명령 줄 도구를 사용하여이 작업을 수행 할 수 있습니다. 그렇게하면 SOAP 인프라에 대해 너무 걱정할 필요없이 메서드를 호출하고 결과를 일반 C# 개체로받을 수 있습니다. 클라이언트 프록시를 일단

,이 같이 보입니다 코드를 작성할 수 있습니다 :

var client = new ServiceReference.ServiceClient(); 
var result = client.GetMetalQuote(); 
System.Diagnostics.Debug.WriteLine(result.GetMetalQuoteResult.Currency); 
// etc. 
0
당신은 결과를 얻기 위해 이것을 사용할 수 있습니다

:

var result = root1.Descendants() 
    .First(x => x.Name.LocalName == "GetMetalQuoteResult") 
    .Elements() 
    .Select(x => new { Name = x.Name.LocalName, Value = x.Value }) 
    .ToArray(); 

가 결과 값을 얻으려면를 :

foreach(var x in result) 
    System.Diagnostics.Debug.WriteLine(x.Name + ": "+ x.Value); 
0
 string url = "http://www..../..."; 
     var xml = XElement.Load(url); 
     XNamespace ns = "http://.../..."; 
     var results = 
      from result in xml.Descendants(ns + "GetMetalQuoteResult") 
      select new SpotPriceModel 
      { 
       Type = result.Element(ns + "Type").Value, 
       Currency = result.Element(ns + "Currency").Value, 
       ... 
       ... 
       Ask = (decimal)result.Element(ns + "Ask"), 
       AskTime = result.Element(ns + "AskTime").Value 
      }; 

     var spot = results.First(); 

     System.Diagnostics.Debug.WriteLine("\n\nASK:\t" + spot.Ask + "\n\n"); 

     return View(spot); 
    } 
+3

코드가하는 일에 대한 설명 es는 정말 좋을 것입니다 ... – Joel

+0

이 게시물은 질문에 대답 할 수 있지만, 설명을 추가하고 관련 문서에 대한 링크를 추가하는 것이 좋습니다. 좋은 설명과 참조가있는 대답은 현재 OP와 미래 방문자에게 더 유용합니다. 완전한 상세한 답변은 또한 긍정적 인 표를 얻는 경향이 있습니다. –