xml - c# windowphone 8.1 code with async task -
i try read xml form url.
code:
static xdocument getxml(string url) { using (httpclient client = new httpclient()) { var response = client.getstreamasync(url); return xdocument.load(response.result); } }
and read xml this:
public object deteailsemp(string emp_xml, string emp_error) { xdocument xml; try { xml = xdocument.parse(getxml(emp_xml).tostring()); } catch { xml = xdocument.load(emp_error); } var detail = query in xml.descendants("emp") select new data.emp { name = (string)query.element("name").value, age = (string)query.element("age").value, return detail ; }
with code, ok.
but, when change code async task this:
async static task<xdocument> getxml(string url) { using (httpclient client = new httpclient()) { var response = await client.getstreamasync(url); return xdocument.load(response); } }
i can't read xml code.
how fix it?
i got lazy earlier , didn't read question properly. lol.
you forgot call await on deteailsemp method (also should converted async)
call this:
object obj = await deteailsemp(param1, param2); // proper call using await
or
object obj = deteailsemp(param1, param2).result; // force result public async task<object> deteailsemp(string emp_xml, string emp_error) { xdocument xml; try { xml = xdocument.parse((await getxml(emp_xml)).tostring()); // since getxml asynchronous method, need result first before calling tostring() or can use getxml(emp_xml).result.tostring() } catch { xml = xdocument.load(emp_error); } var detail = query in xml.descendants("emp") select new data.emp { name = (string)query.element("name").value, age = (string)query.element("age").value, return detail ; } async static task<xdocument> getxml(string url) { using (httpclient client = new httpclient()) { var response = await client.getstreamasync(url); return xdocument.load(response); } }
Comments
Post a Comment