c# - Return XML document with REST API -
i return xml document 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 alclass classid='" + classid.classid + "'").tostring(); xmltextreader reader = new xmltextreader(appdomain.currentdomain.basedirectory + "resource\\" + percorso); reader.read(); doc.load(reader); return doc.innerxml; }
but in way string, have xmldocument not string. tried return xmldocument doc, gives me error: 'objectcontent`1' type failed serialize response body content type 'application/xml; charset=utf-8'. have ideas?
as wrote here few seconds ago (but deleted answer) problem xmldocument not serializable, if use xmlelement instead ok. here 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 alclass classid='" + classid.classid + "'").tostring(); xmltextreader reader = new xmltextreader(appdomain.currentdomain.basedirectory + "resource\\" + percorso); reader.read(); doc.load(reader); xmlelement element = doc.documentelement; return element; }
Comments
Post a Comment