我正在使用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;
}
}
}
答案 0 :(得分:1)
您的问题是XDocument
未实现IXmlSerializable
。相反,请使用 实施IXmlSerializable
的根XElement
:
request.AddBody(currencyXML.Root);
或者,只需将其加载为XElement
即可开始:
var currencyXML = XElement.Load(@"../../Data/currencyXML.xml");