c# - Force JsonConvert.SerializeXmlNode to serialize node value as an Integer or a Boolean -


the serializexmlnode function newtonsoft.json.jsonconvert class outputs value of last child nodes of xml string type in serialization process, when might need them serialized integer or boolean.

sample code:

<object>   <id>12</id>   <title>mytitle</title>   <visible>false</visible> </object> 

output:

{ "id" : "12",   "title" : "mytitle",   "visible" : "false" } 

desired output:

{ "id" : 12,   "title" : "mytitle",   "visible" : false } 

is there way force xml node serialized integer or boolean?

thank you.

note: please avoid posting workarounds when xml serialized json string, workarounds ones willing avoid.

json.net not tool xml serialization. it's serialization of xml nodes meant provide one-to-one correspondence between xml , json. attributes in xml can of type string, type information not preserved during serialization. useless when deserializing json.

if need convert xml json, suggest using dto class supports both xml , json serialization.

[xmlroot ("object"), jsonobject] public class root {     [xmlelement, jsonproperty]     public int id { get; set; }      [xmlelement, jsonproperty]     public string title { get; set; }      [xmlelement, jsonproperty]     public bool visible { get; set; } } 

deserialize xml , serialize json:

public class program {     private const string xml = @"         <object>           <id>12</id>           <title>mytitle</title>           <visible>false</visible>         </object>";      private static void main ()     {         var serializer = new xmlserializer(typeof(root));         var root = (root)serializer.deserialize(new stringreader(xml));         console.writeline(jsonconvert.serializeobject(root, formatting.indented));         console.readkey();     } } 

output:

{   "id": 12,   "title": "mytitle",   "visible": false } 

Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -