asp.net web api2 - LINQ to XML- only return variables and variable values -


good day calling sms client (using c# api v2 [rest])that return xml results follows:

<apiresult>   <data>     <credits>100</credits>   </data>   <callresult>     <result>true</result>     <error />   </callresult> </apiresult> 

using linq xml, return variables object, i.e. credits: 100, result : true , , return json.

i have tried following:

//remove invalid chars var legalchars = removeillegalchars(results);  xdocument po = xdocument.parse(legalchars); var list1 = po.root.descendants("apiresult"); 

without obtaining desired result. appreciated.

you need newtonsoft.json package, , using xelement:

xelement root = xelement.parse(@"     <apiresult>         <data>             <credits>100</credits>         </data>         <callresult>             <result>true</result>             <error />         </callresult> </apiresult>     ");      var credits = root.element("data").element("credits").value;     var result = root.element("callresult").element("result").value;      jobject jsonobj = jobject.fromobject(         new {credits = credits, result = result}     );      console.writeline (jsonobj.tostring()); 

of course instead of writing console, return jsonobj.tostring()


Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -