Restsharp - 将XDocument转换为Object

时间:2017-04-21 06:04:14

标签: c# xml soap linq-to-xml restsharp

我正在使用RestSharp来请求这是一个简单的SOAP服务自动化项目。我通过XDocument加载xml但是我无法在request.AddBody中使用它。

错误:类型' 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 个答案:

答案 0 :(得分:1)

您的问题是XDocument未实现IXmlSerializable。相反,请使用 实施IXmlSerializable的根XElement

request.AddBody(currencyXML.Root);

或者,只需将其加载为XElement即可开始:

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