Reputation: 1251
I would like to return a xml document from rest api request:
[HttpPost]
public string getClassXml(HttpRequestMessage req)
{
var response = Request.CreateResponse(HttpStatusCode.OK);
var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
ClassXML classid = new ClassXML();
XmlDocument doc = new XmlDocument();
try
{
var data = req.Content.ReadAsStringAsync().Result;
classid = serializer.Deserialize<ClassXML>(data.ToString().Trim());
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
string path = ASDb.ReadValue("SELECT definitionxml FROM alclass WHERE classid='" + classid.classID + "'").ToString();
XmlTextReader reader = new XmlTextReader(AppDomain.CurrentDomain.BaseDirectory + "Resource\\" + percorso);
reader.Read();
doc.Load(reader);
return doc.innerXml;
}
But in this way I get a string, I would like to have a XmlDocument not a string. I tried also to return the XmlDocument doc, but it gives me an error: he 'ObjectContent`1' type failed to serialize the response body for content type 'application/xml; charset=utf-8'. Do you have any ideas?
Upvotes: 1
Views: 12263
Reputation: 4801
Perhaps the issue isn't with your API layer, but when you're trying to use your XmlTextReader?
XmlTextReader reader = new XmlTextReader(AppDomain.CurrentDomain.BaseDirectory + "Resource\\" + percorso);
What does your XML that you're trying to read in look like? Have you checked that it's well formed?
In terms of "returning XML document with REST API", I would suggest that you just output the XML document as a string with the appropriate MIME type, doing something like:
[HttpPost]
public HttpResponseMessage getClassXml(HttpRequestMessage req)
{
...
XmlTextReader reader = new XmlTextReader(AppDomain.CurrentDomain.BaseDirectory + "Resource\\" + percorso);
reader.Read();
doc.Load(reader);
HttpResponseMessage response = new HttpResponseMessage { Content = new StringContent(doc.innerXml, Encoding.UTF8,"application/xml") };
return response;
}
REST API outputs should map to standard internet mime types (eg JSON data, images, text, etc - not XmlDocument). Whatever is consuming your REST API can just take the text and turn it into a XmlDocument if necessary.
As an aside, you don't seem to even use half the code in the example you provided and you can probably clean it up:
string path = ASDb.ReadValue("SELECT definitionxml FROM alclass WHERE classid='" + classid.classID + "'").ToString();
Upvotes: 1
Reputation: 1251
Sorry but if I now want to change from HttpPost to HttpGet how would I get the parameters in the following url: http://localhost/arcosat/api/ws/GetClassXml?classid=myclass
I want to get the "myclass" string but with req.Content.ReadAsStringAsync().Result
it doesn't work anymore
Upvotes: 0
Reputation: 1251
As someone wrote here just a few seconds ago (but then deleted his answer) the problem is that XmlDocument is not serializable, if you use XmlElement instead is ok. Here is what I did:
[HttpPost]
public XmlElement getClassXml(HttpRequestMessage req)
{
var response = Request.CreateResponse(HttpStatusCode.OK);
var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
ClassXML classid = new ClassXML();
XmlDocument doc = new XmlDocument();
try
{
var data = req.Content.ReadAsStringAsync().Result;
classid = serializer.Deserialize<ClassXML>(data.ToString().Trim());
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
string path = ASDb.ReadValue("SELECT definitionxml FROM alclass WHERE classid='" + classid.classID + "'").ToString();
XmlTextReader reader = new XmlTextReader(AppDomain.CurrentDomain.BaseDirectory + "Resource\\" + percorso);
reader.Read();
doc.Load(reader);
XmlElement element = doc.DocumentElement;
return element;
}
Upvotes: 5