我是.Net的新手,我在执行以下代码时遇到以下错误。我想将我的响应映射到ESBResponse类并获得响应状态。
One or more errors occurred. --->
System.Runtime.Serialization.SerializationException: Error in line 1 position 14.
Expecting element 'ArrayOfESBResponse'
from namespace ''.. Encountered 'Element' with name 'esbresponse', namespace ''.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.IO;
using System.Diagnostics;
using System.Net.Http.Headers;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.Runtime.Serialization;
namespace MvcApplication2.Controllers
{
[DataContract(Namespace = "")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "")]
public class ESBResponse
{
[DataMember]
public string status { get; set; }
}
public class ValuesController : ApiController
{
// GET api/values
public IEnumerable<string> Get()
{
String url = "http://192.168.10.3:9797";
String xml = "<apprequest><txnid>01472633956983096440</txnid><timestamp>1472633956988</timestamp><phoneno /><rokaid>BAT1234</rokaid><category>Customer</category><RequestType>App</RequestType><IMEINO>359861050082439</IMEINO><appType>IOS</appType><reqIP>192.168.13.201</reqIP></apprequest>";
String response = postXMLData(url, xml);
Debug.WriteLine("RESPONE ####### " + response);
return new string[] { "value1", "value2" };
}
// GET api/values/5
public string Get(int id)
{
return "value";
}
// POST api/values
public void Post([FromBody]string value)
{
}
// PUT api/values/5
public void Put(int id, [FromBody]string value)
{
}
// DELETE api/values/5
public void Delete(int id)
{
}
public string postXMLData(string destinationUrl, string requestXml)
{
/** HttpWebRequest request = (HttpWebRequest)WebRequest.Create(destinationUrl);
byte[] bytes;
bytes = System.Text.Encoding.ASCII.GetBytes(requestXml);
request.ContentType = "text/xml; encoding='utf-8'";
request.ContentLength = bytes.Length;
request.Method = "POST";
Stream requestStream = request.GetRequestStream();
requestStream.Write(bytes, 0, bytes.Length);
requestStream.Close();
HttpWebResponse response;
response = (HttpWebResponse)request.GetResponse();
*/
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(destinationUrl);
// Add an Accept header for JSON format.
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/xml"));
var httpContent = new StringContent(requestXml, Encoding.UTF8, "application/xml");
var response = client.PostAsync("/RokaServices/Api/rokaidavailable", httpContent).Result;
Debug.WriteLine("%%%%%%%%%%%%%%%% " + response);
if (response.IsSuccessStatusCode)
{
try
{
// your code
//Stream responseStream = response.GetResponseStream();
var dataObjects = response.Content.ReadAsAsync<IEnumerable<ESBResponse>>().Result;
foreach (var d in dataObjects)
{
Debug.WriteLine("%%%%%%%%%%%%%%%% " + d.status);
}
}
catch (AggregateException e)
{
Debug.WriteLine("%%%%%%%%%%%%%%%% " + e.ToString());
}
// responseStr = new StreamReader(responseStream).ReadToEnd();
// return responseStr;
}
return null;
}
}
}
<?xml version="1.0" encoding="UTF-8"?><esbresponse><txnid>01472633956983096440</txnid><RequestType>App</RequestType><IMEINO>359861050082439</IMEINO><phoneno/><rokaid>BAT1234</rokaid><category>Customer</category><otp/><isrokaidavailable>true</isrokaidavailable><status>success</status></esbresponse>
答案 0 :(得分:1)
您的错误将在此行中发生:
var dataObjects = response.Content.ReadAsAsync<IEnumerable<ESBResponse>>().Result;
var dataObjects = response.Content.ReadAsAsync<IEnumerable<ESBResponse>>().Result;
您想将结果转换为,但您的回复中不包含ESBResponse列表,我看到的回复只是
**IEnumerable < ESBResponse >**
的一个实例,请尝试强制转换为ESBResponse
这个 :
ESBResponse
response.Content.ReadAsAsync< ESBResponse >().Result;