c# - How to convert stream reader response to a class object? -
i working on functionality makes httppost response.
here's code i'm working with:
public string submitrequest(string posturl, string contenttype, string postvalues) { var req = webrequest.create(posturl); req.method = "post"; req.contenttype = contenttype; try { using (var reqstream = req.getrequeststream()) { var writer = new streamwriter(reqstream); writer.writeline(postvalues); } var resp = req.getresponse(); using (var respstream = resp.getresponsestream()) { var reader = new streamreader(respstream); return reader.readtoend().trim(); } } catch(webexception ex) { // here } return string.empty; }
the function returns xml in string format, instance:
<result> <code>failed</code> <message>duplicate application</message> </result>
this needs converted class object - i'm not sure how go in correct way.
any advice appreciated.
you want deserialize returned xml object. basic example:
//m string based xml representation of object. make sure there's there if (!string.isnullorwhitespace(m)) { //make new xmlserializer type of object being created var ser = new xmlserializer(typeof(yourtype)); //deserialize , cast type of object var obj = (yourtype)ser.deserialize(new stringreader(m)); return obj ; }
Comments
Post a Comment