i using following code add attribute xml designate node should return integer value when using jsonconvert.serializexmlnode.
i have incorporated update newtonsoft referenced dll.
i using following code add attribute:
ele.setattribute("integer", "http://james.newtonking.com/projects/json", "true");
where ele
comes xmlelement ele = node xmlelement;
the result ends this:
"id": { "@type": "integer", "#text": "759263947" },
but need is
"id": 759263947
please note use exact same syntax identify an array:
ele.setattribute("array", "http://james.newtonking.com/projects/json", "true");
which working fine.
laura
if using variant version of xmlnodeconverter
described in this answer , available here: https://github.com/lukegothic/newtonsoft.json/blob/master/src/newtonsoft.json/converters/xmlnodeconverter.cs, looks need do:
ele.setattribute("type", "http://james.newtonking.com/projects/json", "integer");
or, double value:
ele.setattribute("type", "http://james.newtonking.com/projects/json", "float");
alternatively, use linq-to-json out-of-the-box manually modify convert string values numeric values, instance:
string xml = @"<fulfillment xmlns:json=""http://james.newtonking.com/projects/json""><tracking_number>937467375966</tracking_number><tracking_url>google.com/search?q=937467375966</tracking_url>; <line_items json:array=""true""><id>759263947</id><quantity>1.00000</quantity></line_items></fulfillment>"; var doc = new xmldocument(); doc.loadxml(xml); var obj = jobject.parse(jsonconvert.serializexmlnode(doc)); foreach (var value in obj.descendants().oftype<jvalue>().where(v => v.type == jtokentype.string)) { long lval; if (long.tryparse((string)value, out lval)) { value.value = lval; continue; } double dval; if (double.tryparse((string)value, out dval)) { value.value = dval; continue; } decimal dcval; if (decimal.tryparse((string)value, out dcval)) { value.value = dcval; continue; } } var json = obj.tostring(); debug.writeline(json);