2017-04-21 68 views
1

我正在使用RestSharp來請求這是一個簡單的SOAP服務自動化項目。我通過XDocument加載xml,但我不能在request.AddBody中使用它。Restsharp - 將XDocument轉換爲對象

錯誤:「System.InvalidOperationException」類型的異常出現在RestSharpXML.dll但在用戶代碼中沒有處理

public class SOAPSharp 

{ 
     XDocument currencyXML = XDocument.Load(@"../../Data/currencyXML.xml"); 

     [Test] 
     public void xmlRequest() { 

      try 
      { 
       var client = new RestClient(); 

       var request = new RestRequest("http://www.webservicex.net/periodictable.asmx?WSDL", Method.POST); 
       request.XmlSerializer = new RestSharp.Serializers.DotNetXmlSerializer(); 
       request.RequestFormat = DataFormat.Xml; 
       request.AddHeader("Content-Type", "application/xml; charset=utf-8"); 
       // request.AddBody(ParameterType.RequestBody); 
       request.AddBody(currencyXML); 

       var response = client.Execute(request); 
      } 

      catch (Exception) 
      { 
       throw; 
      } 

     } 
    } 

回答

1

您的問題是XDocument沒有實現IXmlSerializable。相反,使用根XElement實現IXmlSerializable

request.AddBody(currencyXML.Root); 

或者,只是加載它作爲一個XElement開始說起:@dbc

var currencyXML = XElement.Load(@"../../Data/currencyXML.xml"); 
+0

謝謝,它的工作原理清楚 –