Convert Json format to xml format -
i have query select data:
public function getstafflist(staffcode string) ienumerable dim query = (from c in staff c.staffid= staffcode select c) return query end function
after using code below return json:
public function getpersonlistjson(personcode string) string return jsonconvert.serializeobject(getstafflist(staffcode)) end function
the json format below:
"[{\"$id\":\"1\",\"personid\":10001.0,\"personname\":\"staff1\"}]"
if want return xml format, how do? thanks
update: try using following code return xml
public function getpersonlistjson(personcode string) string dim json = jsonconvert.serializeobject(getstafflist(staffcode)) dim rootjson = "{""root"":" & json & "}" dim xml = jsonconvert.deserializexnode(rootjson) return xml.tostring() end function
the value of xml during debug is:
<root xmlns:json="http://james.newtonking.com/projects/json" json:id="1"><personid>10001</personid><personname>staff1</personname> <entitykey json:id="2"><entitysetname>tblperson</entitysetname><entitycontainername>personentities</entitycontainername><entitykeyvalues><key>personid</key><type>system.decimal</type><value>10001</value></entitykeyvalues></entitykey></root>
after added .tostring(), return result following:
"\u000d\u000a \u000d\u000a 10001<\/personid>\u000d\u000a staff1<\/personname>\u000d\u000a \u000d\u000a
but not xml format. please again. thanks
you can use deserializexnode
method. based on json you'll need specify root element name xml element. below i've used "staff" root name.
dim xml = jsonconvert.deserializexnode(json, "staff")
the above returns xdocument
. return xml string, add .tostring()
@ end of it. based on comments sounds you're trying add code existing getpersonlistjson
method. add line after 1 above:
return xml.tostring()
that said, method name no longer matches you're doing. it's named getpersonlistjson
you're returning xml string, not json. suggest renaming clarity.
update: sample string json array, why above gives xmlnodeconverter can convert json begins object
error. fix you'll need add root element json manually:
public function getpersonlistjson(personcode string) string dim json = jsonconvert.serializeobject(getstafflist(staffcode)) ' step adds root json (you can rename "root" anything)' dim rootjson = "{""root"":" & json & "}" dim xml = jsonconvert.deserializexnode(rootjson) return xml.tostring() end function
Comments
Post a Comment